Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asyncio: Use default sleep / ensure_future implementations when possible #1376

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/portage/util/futures/_asyncio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def run(coro):
run.__doc__ = _real_asyncio.run.__doc__


def create_subprocess_exec(*args, **kwargs):
def create_subprocess_exec(*args, loop=None, **kwargs):
"""
Create a subprocess.

Expand All @@ -140,7 +140,6 @@ def create_subprocess_exec(*args, **kwargs):
@rtype: asyncio.subprocess.Process (or compatible)
@return: asyncio.subprocess.Process interface
"""
loop = _wrap_loop(kwargs.pop("loop", None))
# Python 3.4 and later implement PEP 446, which makes newly
# created file descriptors non-inheritable by default.
kwargs.setdefault("close_fds", False)
Expand Down Expand Up @@ -186,6 +185,9 @@ def ensure_future(coro_or_future, loop=None):
@rtype: asyncio.Future (or compatible)
@return: an instance of Future
"""
if loop is None:
return _real_asyncio.ensure_future(coro_or_future)

loop = _wrap_loop(loop)
if isinstance(loop._asyncio_wrapper, _AsyncioEventLoop):
# Use the real asyncio loop and ensure_future.
Expand All @@ -210,9 +212,12 @@ def sleep(delay, result=None, loop=None):
@param result: result of the future
@type loop: asyncio.AbstractEventLoop (or compatible)
@param loop: event loop
@rtype: asyncio.Future (or compatible)
@return: an instance of Future
@rtype: collections.abc.Coroutine or asyncio.Future
@return: an instance of Coroutine or Future
"""
if loop is None:
return _real_asyncio.sleep(delay, result=result)

loop = _wrap_loop(loop)
future = loop.create_future()
handle = loop.call_later(delay, future.set_result, result)
Expand Down