Drop-in replacement for nest-asyncio. Allows nested asyncio event loops.
Project description
reloop
Drop-in replacement for nest-asyncio. Allows nested asyncio event loops.
Why reloop?
nest-asyncio has 30M+ downloads/month but was archived in March 2024 after its maintainer passed away. It has known issues with Python 3.12+ (ResourceWarning for unclosed event loops), no way to undo patches, and no support for newer Python internals.
reloop is a maintained, improved replacement:
revert()support — permanently applying patches is optional; you can undo them- Context manager —
with reloop.applied():for scoped patching - Python 3.12+ compatible — no spurious
ResourceWarningfor unclosed event loops - Thread-safe — concurrent
apply()andrevert()calls are safe - Zero dependencies — just stdlib
Installation
pip install reloop
Quick Start
import reloop
reloop.apply()
That's it. Now asyncio.run() and loop.run_until_complete() work inside running event loops — Jupyter notebooks, LLM libraries, web framework sync handlers, and anywhere else Python's RuntimeError: This event loop is already running gets in your way.
Usage
import asyncio
import reloop
reloop.apply()
async def inner():
return 42
async def outer():
# This would normally raise RuntimeError
return asyncio.run(inner())
asyncio.run(outer()) # works
Scoped Patching
with reloop.applied():
asyncio.run(outer()) # patched
asyncio.run(outer()) # raises RuntimeError again
Revert
reloop.apply()
# ... use nested loops ...
reloop.revert() # restore original asyncio behavior
API Reference
reloop.apply() -> None
Patch asyncio to allow nested event loops.
- Idempotent — safe to call multiple times; subsequent calls are no-ops.
- Thread-safe — concurrent calls from multiple threads are safe.
import reloop
reloop.apply()
reloop.revert() -> None
Undo all asyncio patches, restoring original stdlib behavior.
- Idempotent — safe to call multiple times; subsequent calls are no-ops.
- Thread-safe — concurrent calls from multiple threads are safe.
reloop.revert()
reloop.applied() -> contextmanager
Context manager that calls apply() on entry and revert() on exit. Use this when you want nested loop support scoped to a block rather than process-wide.
with reloop.applied():
asyncio.run(outer()) # nested loops work here
asyncio.run(outer()) # raises RuntimeError again after the block
Migration from nest-asyncio
Search and replace:
- import nest_asyncio
- nest_asyncio.apply()
+ import reloop
+ reloop.apply()
Differences from nest-asyncio
revert()support — nest-asyncio patches are permanent; reloop can undo them- Context manager —
with reloop.applied():for scoped patching - Python 3.12+ fixes — no
ResourceWarningfor unclosed event loops - Thread-safe — concurrent
apply()calls are safe - Maintained — nest-asyncio was archived in March 2024
Compatibility
- Python 3.10+
- Zero dependencies
License
MIT
Project details
Release history Release notifications | RSS feed
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 reloop-0.1.0.tar.gz.
File metadata
- Download URL: reloop-0.1.0.tar.gz
- Upload date:
- Size: 31.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80282200177a00dfd547cbe2e25ee077933966ca5774502c53dcdd91ee9e41d8
|
|
| MD5 |
2fac3b38727cdf1801a73bdb50017686
|
|
| BLAKE2b-256 |
77fd67e889a04bb21fa2edb1eca7b0c968c7576d3592310076304457e19e2467
|
File details
Details for the file reloop-0.1.0-py3-none-any.whl.
File metadata
- Download URL: reloop-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1010afc0fc7864663f55a191584b1f54bb3b56579dddefd8fd4236d000a2a49f
|
|
| MD5 |
ea6915341e007c96664539349f41bbf9
|
|
| BLAKE2b-256 |
73f8beea4cf9b82344832aee64b053063f3a9f7647060176be2e069192f04145
|