diff options
author | Zac Medico <zmedico@gentoo.org> | 2018-05-08 21:59:59 -0700 |
---|---|---|
committer | Zac Medico <zmedico@gentoo.org> | 2018-05-08 21:59:59 -0700 |
commit | 920b90fd0883dbd36f0290d08c9af49a208c2950 (patch) | |
tree | d0e98448cd671c7e66bb212ea080d21cf624a48e | |
parent | global_event_loop: use asyncio event loop (bug 654390) (diff) | |
download | portage-920b90fd.tar.gz portage-920b90fd.tar.bz2 portage-920b90fd.zip |
_wrap_loop: default to global_event_loop behavior
The default loop returned by _wrap_loop should be consistent
with global_event_loop, in order to avoid accidental registration
of callbacks with a loop that is not intended to run.
Fixes 96cc07326391 ("global_event_loop: use asyncio event loop (bug 654390)")
-rw-r--r-- | pym/portage/util/futures/_asyncio/__init__.py | 14 | ||||
-rw-r--r-- | pym/portage/util/futures/unix_events.py | 40 |
2 files changed, 10 insertions, 44 deletions
diff --git a/pym/portage/util/futures/_asyncio/__init__.py b/pym/portage/util/futures/_asyncio/__init__.py index 1273afa02..940da4762 100644 --- a/pym/portage/util/futures/_asyncio/__init__.py +++ b/pym/portage/util/futures/_asyncio/__init__.py @@ -35,7 +35,10 @@ portage.proxy.lazyimport.lazyimport(globals(), 'portage.util.futures.unix_events:DefaultEventLoopPolicy', ) from portage.util._eventloop.asyncio_event_loop import AsyncioEventLoop as _AsyncioEventLoop -from portage.util._eventloop.global_event_loop import _asyncio_enabled +from portage.util._eventloop.global_event_loop import ( + _asyncio_enabled, + global_event_loop as _global_event_loop, +) from portage.util.futures.futures import ( CancelledError, Future, @@ -168,14 +171,15 @@ def _wrap_loop(loop=None): @rtype: asyncio.AbstractEventLoop (or compatible) @return: event loop """ - return loop or get_event_loop() + return loop or _global_event_loop() if _asyncio_enabled: - get_event_loop_policy = _real_asyncio.get_event_loop_policy - set_event_loop_policy = _real_asyncio.set_event_loop_policy + # The default loop returned by _wrap_loop should be consistent + # with global_event_loop, in order to avoid accidental registration + # of callbacks with a loop that is not intended to run. def _wrap_loop(loop=None): - loop = loop or get_event_loop() + loop = loop or _global_event_loop() return (loop if hasattr(loop, '_asyncio_wrapper') else _AsyncioEventLoop(loop=loop)) diff --git a/pym/portage/util/futures/unix_events.py b/pym/portage/util/futures/unix_events.py index ce520db00..8eb369f8b 100644 --- a/pym/portage/util/futures/unix_events.py +++ b/pym/portage/util/futures/unix_events.py @@ -681,42 +681,4 @@ class _PortageEventLoopPolicy(events.AbstractEventLoopPolicy): return _global_event_loop()._asyncio_child_watcher -class _AsyncioEventLoopPolicy(_PortageEventLoopPolicy): - """ - Implementation of asyncio.AbstractEventLoopPolicy based on asyncio's - event loop. This supports running event loops in forks, - which is not supported by the default asyncio event loop policy, - see https://bugs.python.org/issue22087 and also - https://bugs.python.org/issue29703 which affects pypy3-5.10.1. - """ - _MAIN_PID = os.getpid() - - def __init__(self): - super(_AsyncioEventLoopPolicy, self).__init__() - self._default_policy = _real_asyncio.DefaultEventLoopPolicy() - - def get_event_loop(self): - """ - Get the event loop for the current context. - - Returns an event loop object implementing the AbstractEventLoop - interface. - - @rtype: asyncio.AbstractEventLoop (or compatible) - @return: the current event loop policy - """ - if os.getpid() == self._MAIN_PID: - return self._default_policy.get_event_loop() - else: - return super(_AsyncioEventLoopPolicy, self).get_event_loop() - - def get_child_watcher(self): - """Get the watcher for child processes.""" - if os.getpid() == self._MAIN_PID: - return self._default_policy.get_child_watcher() - else: - return super(_AsyncioEventLoopPolicy, self).get_child_watcher() - - -DefaultEventLoopPolicy = (_AsyncioEventLoopPolicy if _asyncio_enabled - else _PortageEventLoopPolicy) +DefaultEventLoopPolicy = _PortageEventLoopPolicy |