Package: backports.asyncio.runner
backports.asyncio.runnerThis is a backport of Python 3.11asyncio.Runner, a context manager that simplifies multiple async
function calls in the same context.This backports provides full compatibility with Python 3.11asyncio.Runnerfeatures, includingcontextvars.Contextsupport.
As such, the Python 3.11 test suite forasyncio.Runneris used as-is to test its functionality.This backport is meant to work with Python 3.8, 3.9, and 3.10 only. Users of Python 3.11 or above should
not use this package directly.To install, you can usepython -m pip install backports.asyncio.runneror your favorite python package manager.
You might need to include a marker (e.g.python_version < '3.11') to prevent installation on Python 3.11 or above.An example of the recommended way to use this context manager is in the below code snippet:importsysifsys.version_info<(3,11):frombackports.asyncio.runnerimportRunnerelse:fromasyncioimportRunnerasyncdefecho(msg:str)->None:print(f"Hello{msg}")withRunner()asrunner:runner.run(echo("World"))# Hello WorlduvloopThis backport is also compatible withuvloop. An example is below:importsysimportuvloopifsys.version_info<(3,11):frombackports.asyncio.runnerimportRunnerelse:fromasyncioimportRunnerasyncdefecho(msg:str)->None:print(f"Hello{msg}")withRunner(loop_factory=uvloop.new_event_loop)asrunner:runner.run(echo("World"))# Hello WorldContributingFeel free to open a PR if there's changes you'd like to make as long as the changes maintain full reference implementation
with Python 3.11's implementation ofasyncio.Runner. More documentation and additional tests are always welcome as CPython's
test don't seem to provide 100% coverage at a glance or more likely I may have missed including some tests.ruffis used to format the code viaruff format.To quickly run ruff, make surepipxis installed an runpipx run ruff==0.1.8 format src testsmypyis used to check types in the sources (while attempting to stay true to reference implementation).To quickly run mypy, make surepipxis installed an runpipx run mypy==1.7.1 srcTo run tests usepython -m unittest discover -s tests.To gather coverage:Install coverage:python -m pip install "coverage[toml]==7.3.3"Run it:coverage run -m unittest discover -s testsRelevant reference implementation sources:https://github.com/python/cpython/blob/3.11/Lib/asyncio/runners.pyhttps://github.com/python/cpython/blob/3.11/Lib/asyncio/tasks.pyhttps://github.com/python/cpython/blob/3.11/Lib/asyncio/base_events.pyhttps://github.com/python/cpython/blob/3.11/Modules/_asynciomodule.chttps://github.com/python/cpython/blob/3.11/Lib/test/test_asyncio/test_runners.pyhttps://github.com/python/cpython/blob/3.8/Lib/test/test_asyncio/test_tasks.pyhttps://github.com/python/cpython/blob/3.9/Lib/test/test_asyncio/test_tasks.pyhttps://github.com/python/cpython/blob/3.10/Lib/test/test_asyncio/test_tasks.pyCaveatsThis implementation usesasyncio.tasks._PyTaskinstead ofasyncio.tasks._CTaskas it adds additional functionality toasyncio.Taskin order to supportasyncio.Runnerrequirements. Hence, theasyncio.TaskimplementationRunnerwill
use also comes from this package. As such, problems can arise when checkingisinstance(some_runner_main_task, asyncio.Task)sinceasyncio.Taskcan point to_CTaskby default instead of_PyTask. You may encounter the same issue when comparing
it toasyncio.Future.To guarantee fullasyncio.Taskfunctionality, upstream tests forasyncio.Taskfor 3.8, 3.9, and 3.10 are also part of the
testing suite of this package to make sure all changes toasyncio.Taskused by theRunnerare fully compatible withasyncio.Task. Important Note:asyncio.Taskisonlypatched on theRunner, not globally, hence there should be
no side effects to external code when using this module.Currently, a backport of_CTaskis not provided on this package and not in scope at this time. This means that there's a slight
performance degradation when using thisasyncio.Runnerimplementation over the one in Python 3.11 or above.1.0.2Adduvloopexample to README.mdImproved tests coverage.Improved modified code documentation.1.0.1requires-pythonmetadata is now properly set to ">=3.8,<3.11"1.0.0Initial Reference Implementation