Backport of Python 3.11's `asyncio.Runner`, a context manager that simplifies managing the asyncio event loop lifecycle for Python versions 3.8, 3.9, and 3.10. It provides full compatibility with Python 3.11 `asyncio.Runner` features, including contextvars support. Version 1.2.0 is current, with recent releases indicating an active maintenance cadence.
pip install backports-asyncio-runnerVerified import paths — ran on the pinned version, not inferred.
This example demonstrates the basic usage of `Runner` as a context manager to execute an asynchronous coroutine. It includes a conditional import to ensure compatibility across Python versions where `Runner` might be native or a backport.
Ensure installation is guarded by `python_version < '3.11'` markers or simply avoid installing on Python 3.11+.
Be aware of this internal difference; if type checking `asyncio.Task` or `asyncio.Future` is critical, consider testing the specific behavior in your environment or refactoring to avoid strict type checks against the base `asyncio.Task`/`Future` type when tasks are managed by `backports.asyncio.runner`.
This is an inherent limitation of the backport. For maximum asyncio performance, upgrading to Python 3.11 or newer is recommended.
Use `pip install 'backports-asyncio-runner; python_version < "3.11"'` or `install_requires=['backports-asyncio-runner; python_version < "3.11"']`.
Install the package using pip: `pip install backports-asyncio-runner`.
For Python 3.8, 3.9, or 3.10, install `backports-asyncio-runner` and import `Runner` from the backports package: `from backports.asyncio.runner import Runner`.
To safely check the type, consider checking `isinstance(some_runner_main_task, (asyncio.Task, backports.asyncio.runner.Task))` or avoid direct type checking where possible, relying on duck typing or the expected behavior of tasks within the `Runner` context.