-
-
Notifications
You must be signed in to change notification settings - Fork 719
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
configure asyncio loop using loop_factory kwarg rather than using the set_event_loop_policy #7969
Merged
graingert
merged 3 commits into
dask:main
from
graingert:use-loop-factory-to-set-loop-type
Jul 13, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
import random | ||
import sys | ||
import warnings | ||
from collections.abc import Callable, Coroutine | ||
from typing import Any, TypeVar | ||
|
||
import tornado | ||
|
||
|
@@ -48,7 +50,7 @@ def randbytes(*args, **kwargs): | |
# takes longer than the interval | ||
import datetime | ||
import math | ||
from collections.abc import Awaitable, Callable | ||
from collections.abc import Awaitable | ||
from inspect import isawaitable | ||
|
||
from tornado.ioloop import IOLoop | ||
|
@@ -182,3 +184,84 @@ def _update_next(self, current_time: float) -> None: | |
# time.monotonic(). | ||
# https://github.com/tornadoweb/tornado/issues/2333 | ||
self._next_timeout += callback_time_sec | ||
|
||
|
||
_T = TypeVar("_T") | ||
|
||
if sys.version_info >= (3, 12): | ||
asyncio_run = asyncio.run | ||
elif sys.version_info >= (3, 11): | ||
|
||
def asyncio_run( | ||
main: Coroutine[Any, Any, _T], | ||
*, | ||
debug: bool = False, | ||
loop_factory: Callable[[], asyncio.AbstractEventLoop] | None = None, | ||
) -> _T: | ||
# asyncio.run from Python 3.12 | ||
# https://docs.python.org/3/license.html#psf-license | ||
with asyncio.Runner(debug=debug, loop_factory=loop_factory) as runner: | ||
return runner.run(main) | ||
|
||
else: | ||
# modified version of asyncio.run from Python 3.10 to add loop_factory kwarg | ||
# https://docs.python.org/3/license.html#psf-license | ||
def asyncio_run( | ||
main: Coroutine[Any, Any, _T], | ||
*, | ||
debug: bool = False, | ||
loop_factory: Callable[[], asyncio.AbstractEventLoop] | None = None, | ||
) -> _T: | ||
try: | ||
asyncio.get_running_loop() | ||
except RuntimeError: | ||
pass | ||
else: | ||
raise RuntimeError( | ||
"asyncio.run() cannot be called from a running event loop" | ||
) | ||
|
||
if not asyncio.iscoroutine(main): | ||
raise ValueError(f"a coroutine was expected, got {main!r}") | ||
|
||
if loop_factory is None: | ||
loop = asyncio.new_event_loop() | ||
else: | ||
loop = loop_factory() | ||
try: | ||
if loop_factory is None: | ||
asyncio.set_event_loop(loop) | ||
if debug is not None: | ||
loop.set_debug(debug) | ||
return loop.run_until_complete(main) | ||
finally: | ||
try: | ||
_cancel_all_tasks(loop) | ||
loop.run_until_complete(loop.shutdown_asyncgens()) | ||
loop.run_until_complete(loop.shutdown_default_executor()) | ||
finally: | ||
if loop_factory is None: | ||
asyncio.set_event_loop(None) | ||
Comment on lines
+243
to
+244
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
loop.close() | ||
|
||
def _cancel_all_tasks(loop: asyncio.AbstractEventLoop) -> None: | ||
to_cancel = asyncio.all_tasks(loop) | ||
if not to_cancel: | ||
return | ||
|
||
for task in to_cancel: | ||
task.cancel() | ||
|
||
loop.run_until_complete(asyncio.gather(*to_cancel, return_exceptions=True)) | ||
|
||
for task in to_cancel: | ||
if task.cancelled(): | ||
continue | ||
if task.exception() is not None: | ||
loop.call_exception_handler( | ||
{ | ||
"message": "unhandled exception during asyncio.run() shutdown", | ||
"exception": task.exception(), | ||
"task": task, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this not necessary when using a loop_factory?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the behaviour from 3.11+ when loop_factory was introduced to asyncio.Runner
https://github.com/python/cpython/blob/b03755a2347325a89a48b08fc158419000513bcb/Lib/asyncio/runners.py#L136-L142
it isn't called when the loop_factory is specified because otherwise it would call
asyncio.get_event_loop_policy().set_event_loop(loop)
on the default policy with a loop from another policy which isn't supportedset_event_loop
is currently only used by the posix _UnixDefaultEventLoopPolicy to set an eventloop for listening to child process events with a child process watcher, which is deprecated and not used on windows or uvloop where we use the loop_factory kwarg