Skip to main content

backports.asyncio.runner

PyPI version GitHub Actions Hatch Ruff Uv Type checked with mypy Coverage

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 run ruff, make sure uv is installed and run uv run ruff format src tests
    • To run ruff on applicable python versions, run uv run nox -rs fmt
  • mypy is used to check types in the sources (while attempting to stay true to reference implementation).
    • To run mypy, make sure uv is installed and run uv run mypy src
    • To run mypy on applicable python versions, run uv run nox -rs ty
  • To run tests use uv run python -m unittest discover -s tests
    • For applicable python versions uv run nox -rs tests
  • To gather coverage use uv run coverage run -m unittest discover -s tests
    • For applicable python versions uv run nox -rs tests -- --cov

Relevant reference implementation sources:

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.2.0

1.1.0

  • Add basic noxfile with uv backend.
  • Simplified patching implementation.
  • Test dependency updates.

1.0.2

  • Add uvloop example to README.md
  • Improved tests coverage.
  • Improved modified code documentation.

1.0.1

  • requires-python metadata is now properly set to ">=3.8,<3.11"

1.0.0

  • Initial Reference Implementation

Metadata

Release files for backports.asyncio.runner 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for backports.asyncio.runner 1.2.0
File Size Uploaded
backports_asyncio_runner-1.2.0.tar.gz 69.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for backports.asyncio.runner 1.2.0
File Interpreter ABI Platform
backports_asyncio_runner-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 82.2 kB

Release files / backports_asyncio_runner-1.2.0.tar.gz

Download URL backports_asyncio_runner-1.2.0.tar.gz
Size 69.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a5aa7b2b7d8f8bfcaa2b57313f70792df84e32a2a746f585213373f900b42162
BLAKE2b-256 checksum
How to use checksums
8eff70dca7d7cb1cbc0edb2c6cc0c38b65cba36cccc491eca64cabd5fe7f8670
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 2, 2025.

Transparency log

Release files / backports_asyncio_runner-1.2.0-py3-none-any.whl

Download URL backports_asyncio_runner-1.2.0-py3-none-any.whl
Size 12.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0da0a936a8aeb554eccb426dc55af3ba63bcdc69fa1a600b5bb305413a4477b5
BLAKE2b-256 checksum
How to use checksums
a05976ab57e3fe74484f48a53f8e337171b4a2349e506eabe136d7e01d059086
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 2, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.0

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page