backports.asyncio.runner
This is a backport of Python 3.11 asyncio.Runner, a context manager that simplifies multiple async function calls in the same context.
This backports provides full compatibility with Python 3.11 asyncio.Runner features, including contextvars.Context support.
As such, the Python 3.11 test suite for asyncio.Runner is 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 use python -m pip install backports.asyncio.runner or 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:
import sys
if sys.version_info < (3, 11):
from backports.asyncio.runner import Runner
else:
from asyncio import Runner
async def echo(msg: str) -> None:
print(f"Hello {msg}")
with Runner() as runner:
runner.run(echo("World"))
# Hello World
uvloop
This backport is also compatible with uvloop. An example is below:
import sys
import uvloop
if sys.version_info < (3, 11):
from backports.asyncio.runner import Runner
else:
from asyncio import Runner
async def echo(msg: str) -> None:
print(f"Hello {msg}")
with Runner(loop_factory=uvloop.new_event_loop) as runner:
runner.run(echo("World"))
# Hello World
Contributing
Feel 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 of asyncio.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.
- ruff is used to format the code via
ruff format.- To quickly run ruff, make sure
pipxis installed an runpipx run ruff==0.6.1 format src tests
- To quickly run ruff, make sure
- mypy is used to check types in the sources (while attempting to stay true to reference implementation).
- To quickly run mypy, make sure
pipxis installed an runpipx run mypy==1.11.1 src
- To quickly run mypy, make sure
- To run tests use
python -m unittest discover -s tests. - To gather coverage:
- Install coverage:
python -m pip install "coverage[toml]==7.6.1" - Run it:
coverage run -m unittest discover -s tests
- Install coverage:
Relevant reference implementation sources:
- https://github.com/python/cpython/blob/3.11/Lib/asyncio/runners.py
- https://github.com/python/cpython/blob/3.11/Lib/asyncio/tasks.py
- https://github.com/python/cpython/blob/3.11/Lib/asyncio/base_events.py
- https://github.com/python/cpython/blob/3.11/Modules/_asynciomodule.c
- https://github.com/python/cpython/blob/3.11/Lib/test/test_asyncio/test_runners.py
- https://github.com/python/cpython/blob/3.8/Lib/test/test_asyncio/test_tasks.py
- https://github.com/python/cpython/blob/3.9/Lib/test/test_asyncio/test_tasks.py
- https://github.com/python/cpython/blob/3.10/Lib/test/test_asyncio/test_tasks.py
Caveats
This implementation uses asyncio.tasks._PyTask instead of asyncio.tasks._CTask as it adds additional functionality to
asyncio.Task in order to support asyncio.Runner requirements. Hence, the asyncio.Task implementation Runner will
use also comes from this package. As such, problems can arise when checking isinstance(some_runner_main_task, asyncio.Task)
since asyncio.Task can point to _CTask by default instead of _PyTask. You may encounter the same issue when comparing
it to asyncio.Future.
To guarantee full asyncio.Task functionality, upstream tests for asyncio.Task for 3.8, 3.9, and 3.10 are also part of the
testing suite of this package to make sure all changes to asyncio.Task used by the Runner are fully compatible with
asyncio.Task. Important Note: asyncio.Task is only patched on the Runner, not globally, hence there should be
no side effects to external code when using this module.
Currently, a backport of _CTask is not provided on this package and not in scope at this time. This means that there's a slight
performance degradation when using this asyncio.Runner implementation over the one in Python 3.11 or above.
1.1.0
- Add basic
noxfilewith uv backend. - Simplified patching implementation.
- Test dependency updates.
1.0.2
- Add
uvloopexample to README.md - Improved tests coverage.
- Improved modified code documentation.
1.0.1
requires-pythonmetadata is now properly set to ">=3.8,<3.11"
1.0.0
- Initial Reference Implementation
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file backports_asyncio_runner-1.1.0.tar.gz.
File metadata
- Download URL: backports_asyncio_runner-1.1.0.tar.gz
- Upload date:
- Size: 68.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ed9f8ced96274317b3c56ab5e4f8b18826085a925b0288d9e0e4e3b14e37eb5
|
|
| MD5 |
c9dc5e0d80cde4ff6423205a601cc825
|
|
| BLAKE2b-256 |
6314803c7b93806b8c6887ad7828168b9af4b05ee91a802978b54be1f9dfb9c1
|
File details
Details for the file backports_asyncio_runner-1.1.0-py3-none-any.whl.
File metadata
- Download URL: backports_asyncio_runner-1.1.0-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
926aad145d0a3b044bde35e22092f3a791565609ef28aa1c777f2a5c9e0789e8
|
|
| MD5 |
bcebd9828fa248976c3c8ea7bdcc6605
|
|
| BLAKE2b-256 |
5858d8a942901bff1e904592d224d49f11008338061ddaf25df8baa6273e1a62
|