-
-
Notifications
You must be signed in to change notification settings - Fork 746
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use asyncio.run(..., loop_factory) to avoid asyncio.set_event_loop_po…
…licy
- Loading branch information
Showing
9 changed files
with
141 additions
and
29 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
import sys | ||
from collections.abc import Callable, Coroutine | ||
from typing import Any, TypeVar | ||
|
||
_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) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,19 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
import logging | ||
import sys | ||
from collections.abc import Callable | ||
from typing import TypeVar | ||
|
||
_T = TypeVar("_T") | ||
|
||
logger = logging.getLogger("uvicorn.error") | ||
|
||
|
||
def asyncio_setup(use_subprocess: bool = False) -> None: | ||
if sys.platform == "win32" and use_subprocess: | ||
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) | ||
def asyncio_loop_factory( | ||
use_subprocess: bool = False, | ||
) -> Callable[[], asyncio.AbstractEventLoop]: | ||
if sys.platform == "win32" and not use_subprocess: | ||
return asyncio.ProactorEventLoop | ||
return asyncio.SelectorEventLoop |
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 |
---|---|---|
@@ -1,11 +1,19 @@ | ||
def auto_loop_setup(use_subprocess: bool = False) -> None: | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
from collections.abc import Callable | ||
|
||
|
||
def auto_loop_factory( | ||
use_subprocess: bool = False, | ||
) -> Callable[[], asyncio.AbstractEventLoop]: | ||
try: | ||
import uvloop # noqa | ||
except ImportError: # pragma: no cover | ||
from uvicorn.loops.asyncio import asyncio_setup as loop_setup | ||
from uvicorn.loops.asyncio import asyncio_loop_factory as loop_factory | ||
|
||
loop_setup(use_subprocess=use_subprocess) | ||
return loop_factory(use_subprocess=use_subprocess) | ||
else: # pragma: no cover | ||
from uvicorn.loops.uvloop import uvloop_setup | ||
from uvicorn.loops.uvloop import uvloop_loop_factory | ||
|
||
uvloop_setup(use_subprocess=use_subprocess) | ||
return uvloop_loop_factory(use_subprocess=use_subprocess) |
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 |
---|---|---|
@@ -1,7 +1,12 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
from collections.abc import Callable | ||
|
||
import uvloop | ||
|
||
|
||
def uvloop_setup(use_subprocess: bool = False) -> None: | ||
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) | ||
def uvloop_loop_factory( | ||
use_subprocess: bool = False, | ||
) -> Callable[[], asyncio.AbstractEventLoop]: | ||
return uvloop.new_event_loop |
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