|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import sys |
| 5 | +from collections.abc import Callable, Coroutine |
| 6 | +from typing import Any, TypeVar |
| 7 | + |
| 8 | +__all__ = ["EventLoop", "asyncio_run"] |
| 9 | + |
| 10 | +_T = TypeVar("_T") |
| 11 | + |
| 12 | +if sys.version_info >= (3, 13): |
| 13 | + from asyncio import EventLoop |
| 14 | +elif sys.platform == "win32": |
| 15 | + from asyncio import ProactorEventLoop as EventLoop |
| 16 | +else: |
| 17 | + from asyncio import SelectorEventLoop as EventLoop |
| 18 | + |
| 19 | +if sys.version_info >= (3, 12): |
| 20 | + asyncio_run = asyncio.run |
| 21 | +elif sys.version_info >= (3, 11): |
| 22 | + |
| 23 | + def asyncio_run( |
| 24 | + main: Coroutine[Any, Any, _T], |
| 25 | + *, |
| 26 | + debug: bool = False, |
| 27 | + loop_factory: Callable[[], asyncio.AbstractEventLoop] | None = None, |
| 28 | + ) -> _T: |
| 29 | + # asyncio.run from Python 3.12 |
| 30 | + # https://docs.python.org/3/license.html#psf-license |
| 31 | + with asyncio.Runner(debug=debug, loop_factory=loop_factory) as runner: |
| 32 | + return runner.run(main) |
| 33 | + |
| 34 | +else: |
| 35 | + # modified version of asyncio.run from Python 3.10 to add loop_factory kwarg |
| 36 | + # https://docs.python.org/3/license.html#psf-license |
| 37 | + def asyncio_run( |
| 38 | + main: Coroutine[Any, Any, _T], |
| 39 | + *, |
| 40 | + debug: bool = False, |
| 41 | + loop_factory: Callable[[], asyncio.AbstractEventLoop] | None = None, |
| 42 | + ) -> _T: |
| 43 | + try: |
| 44 | + asyncio.get_running_loop() |
| 45 | + except RuntimeError: |
| 46 | + pass |
| 47 | + else: |
| 48 | + raise RuntimeError("asyncio.run() cannot be called from a running event loop") |
| 49 | + |
| 50 | + if not asyncio.iscoroutine(main): |
| 51 | + raise ValueError(f"a coroutine was expected, got {main!r}") |
| 52 | + |
| 53 | + if loop_factory is None: |
| 54 | + loop = asyncio.new_event_loop() |
| 55 | + else: |
| 56 | + loop = loop_factory() |
| 57 | + try: |
| 58 | + if loop_factory is None: |
| 59 | + asyncio.set_event_loop(loop) |
| 60 | + if debug is not None: |
| 61 | + loop.set_debug(debug) |
| 62 | + return loop.run_until_complete(main) |
| 63 | + finally: |
| 64 | + try: |
| 65 | + _cancel_all_tasks(loop) |
| 66 | + loop.run_until_complete(loop.shutdown_asyncgens()) |
| 67 | + loop.run_until_complete(loop.shutdown_default_executor()) |
| 68 | + finally: |
| 69 | + if loop_factory is None: |
| 70 | + asyncio.set_event_loop(None) |
| 71 | + loop.close() |
| 72 | + |
| 73 | + def _cancel_all_tasks(loop: asyncio.AbstractEventLoop) -> None: |
| 74 | + to_cancel = asyncio.all_tasks(loop) |
| 75 | + if not to_cancel: |
| 76 | + return |
| 77 | + |
| 78 | + for task in to_cancel: |
| 79 | + task.cancel() |
| 80 | + |
| 81 | + loop.run_until_complete(asyncio.gather(*to_cancel, return_exceptions=True)) |
| 82 | + |
| 83 | + for task in to_cancel: |
| 84 | + if task.cancelled(): |
| 85 | + continue |
| 86 | + if task.exception() is not None: |
| 87 | + loop.call_exception_handler( |
| 88 | + { |
| 89 | + "message": "unhandled exception during asyncio.run() shutdown", |
| 90 | + "exception": task.exception(), |
| 91 | + "task": task, |
| 92 | + } |
| 93 | + ) |
0 commit comments