Skip to main content

anest-asyncio

Build PyPi Number of downloads

anest-asyncio is the maintained continuation of nest_asyncio, published under a new name by the ib-api-reloaded project alongside aeventkit and ib_async. It keeps the original import name and public API, so existing code keeps working unchanged:

import nest_asyncio
nest_asyncio.apply()

It supports current Python releases (CPython 3.11 through 3.15, and PyPy 3.11), including Python 3.14’s task and event-loop changes.

Install either anest-asyncio or the original nest_asyncio, not both: they provide the same nest_asyncio module.

Introduction

By design asyncio does not allow its event loop to be nested. This presents a practical problem: When in an environment where the event loop is already running it’s impossible to run tasks and wait for the result. Trying to do so will give the error “RuntimeError: This event loop is already running”.

The issue pops up in various environments, such as web servers, GUI applications and in Jupyter notebooks.

This module patches asyncio to allow nested use of asyncio.run and loop.run_until_complete.

Recent compatibility work in this repository adapts ideas first explored in Chaoses-Ib/nest-asyncio2, then rewrites them for the nest_asyncio codebase and packaging model. The first imported idea is Python 3.12+ support for asyncio.run(..., loop_factory=...). The second is Python 3.14 mixed-task handling when apply() runs inside an already-active task. The third is draining call_soon callbacks scheduled during nested task completion before run_until_complete() returns.

Installation

pip install anest-asyncio

Python 3.11 or higher is required.

Usage

import nest_asyncio
nest_asyncio.apply()

Optionally the specific loop that needs patching can be given as argument to apply, otherwise the current event loop is used. An event loop can be patched whether it is already running or not. Only event loops from asyncio can be patched; Loops from other projects, such as uvloop or quamash, generally can’t be patched.

On Python 3.12 and later, apply() without a running or current loop does not create one: it patches asyncio itself, and the patched asyncio.run patches every loop it uses before running on it.

The patched asyncio.run(main) differs from the standard one so that it can nest:

  • it runs main on the running or current event loop (creating and setting one when there is none) and leaves that loop open afterwards;

  • with loop_factory=... (Python 3.12+) it runs main on a new loop from the factory, as the standard asyncio.run does: that loop is not made the current loop, and is closed afterwards;

  • debug defaults to None: the loop’s debug mode (and PYTHONASYNCIODEBUG / -X dev) is left as it is unless debug is given.

Development

This project is tested on CPython 3.11, 3.12, 3.13, 3.14, 3.15 and PyPy 3.11.

Using uv:

uv sync --group dev
make test         # default interpreter, only the package installed
make test-matrix  # every tested Python, one isolated environment each
make lint         # ruff + mypy

Releasing

Releases are made from main on a maintainer machine with releasekit (configured under [tool.releasekit] in pyproject.toml); CI does not publish. The version is set in one place, [project].version in pyproject.toml: the package metadata, the release checks and the git tag (v<version>) all come from it.

make release-check    # every reason not to publish this version now
make publish-dry-run  # everything local; prints the upload and tag steps
make publish          # upload, tag, verify the release from PyPI

Acknowledgements

Some recent compatibility fixes and regression tests were adapted from Chaoses-Ib/nest-asyncio2 after review and then integrated back into this core project. The code here intentionally keeps the original nest_asyncio public interface while incorporating the useful runtime fixes discovered in that fork.

Metadata

Release files for anest-asyncio 2.0.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 anest-asyncio 2.0.0
File Size Uploaded Provenance Actions
anest_asyncio-2.0.0.tar.gz 10.7 kB Without provenance Details for anest_asyncio-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for anest-asyncio 2.0.0
File Interpreter ABI Platform Actions
anest_asyncio-2.0.0-py3-none-any.whl Python 3 none any Details for anest_asyncio-2.0.0-py3-none-any.whl

Total release size: 19.8 kB

Release files / anest_asyncio-2.0.0.tar.gz

Download URL anest_asyncio-2.0.0.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
01f09e29bc48e47136319582c33bc07826450f033b285d89e01b271af73f0015
BLAKE2b-256 checksum
How to use checksums
bef4e7e791f777b60a02baf8134a4d5ae3355a229716004742b7bd38bee40952
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / anest_asyncio-2.0.0-py3-none-any.whl

Download URL anest_asyncio-2.0.0-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b9c866b7ca604d93360f579e0bb001f7560ccde06849bd7b0624ff011ffa288b
BLAKE2b-256 checksum
How to use checksums
fed10d0985c664acf9e791ca27c622f7a75fb403d0801f6b888825862cc6713b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for anest-asyncio 2.0.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files No provenance

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