aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2019-05-11 23:51:49 -0700
committerZac Medico <zmedico@gentoo.org>2019-05-12 00:02:03 -0700
commit00340dfa7d85144aa975079d6eeebaa035178208 (patch)
tree9e18d631ebfcea0f2211fbc501d298706c3963b2
parentrepoman: check IUSE in _match_use for USE defaults (bug 685482) (diff)
downloadgentoo-portage-00340dfa7d85144aa975079d6eeebaa035178208.tar.xz
gentoo-portage-00340dfa7d85144aa975079d6eeebaa035178208.zip
portdbapi._event_loop: split out _safe_loop function
Signed-off-by: Zac Medico <zmedico@gentoo.org>
-rw-r--r--lib/portage/dbapi/porttree.py10
-rw-r--r--lib/portage/util/futures/_asyncio/__init__.py18
2 files changed, 19 insertions, 9 deletions
diff --git a/lib/portage/dbapi/porttree.py b/lib/portage/dbapi/porttree.py
index 7a28f5876..edff0c2f2 100644
--- a/lib/portage/dbapi/porttree.py
+++ b/lib/portage/dbapi/porttree.py
@@ -34,7 +34,6 @@ from portage import eclass_cache, \
from portage import os
from portage import _encodings
from portage import _unicode_encode
-from portage.util._eventloop.EventLoop import EventLoop
from portage.util.futures import asyncio
from portage.util.futures.compat_coroutine import coroutine, coroutine_return
from portage.util.futures.iter_completed import iter_gather
@@ -346,14 +345,7 @@ class portdbapi(dbapi):
@property
def _event_loop(self):
- if portage._internal_caller:
- # For internal portage usage, asyncio._wrap_loop() is safe.
- return asyncio._wrap_loop()
- else:
- # For external API consumers, use a local EventLoop, since
- # we don't want to assume that it's safe to override the
- # global SIGCHLD handler.
- return EventLoop(main=False)
+ return asyncio._safe_loop()
def _create_pregen_cache(self, tree):
conf = self.repositories.get_repo_for_location(tree)
diff --git a/lib/portage/util/futures/_asyncio/__init__.py b/lib/portage/util/futures/_asyncio/__init__.py
index 2a637624d..e77c7a690 100644
--- a/lib/portage/util/futures/_asyncio/__init__.py
+++ b/lib/portage/util/futures/_asyncio/__init__.py
@@ -37,6 +37,7 @@ import portage
portage.proxy.lazyimport.lazyimport(globals(),
'portage.util.futures.unix_events:_PortageEventLoopPolicy',
'portage.util.futures:compat_coroutine@_compat_coroutine',
+ 'portage.util._eventloop.EventLoop:EventLoop@_EventLoop',
)
from portage.util._eventloop.asyncio_event_loop import AsyncioEventLoop as _AsyncioEventLoop
from portage.util._eventloop.global_event_loop import (
@@ -250,3 +251,20 @@ if _asyncio_enabled:
loop = loop or _global_event_loop()
return (loop if hasattr(loop, '_asyncio_wrapper')
else _AsyncioEventLoop(loop=loop))
+
+
+def _safe_loop():
+ """
+ Return an event loop that's safe to use within the current context.
+ For portage internal callers, this returns a globally shared event
+ loop instance. For external API consumers, this constructs a
+ temporary event loop instance that's safe to use in a non-main
+ thread (it does not override the global SIGCHLD handler).
+
+ @rtype: asyncio.AbstractEventLoop (or compatible)
+ @return: event loop instance
+ """
+ if portage._internal_caller:
+ return _wrap_loop()
+ else:
+ return _EventLoop(main=False)