Skip to main content

Drop-in replacement for nest-asyncio. Allows nested asyncio event loops.

Project description

reloop

CI PyPI Python

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 managerwith reloop.applied(): for scoped patching
  • Python 3.12+ compatible — no spurious ResourceWarning for unclosed event loops
  • Thread-safe — concurrent apply() and revert() 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 managerwith reloop.applied(): for scoped patching
  • Python 3.12+ fixes — no ResourceWarning for 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reloop-0.1.0.tar.gz (31.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reloop-0.1.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

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

Hashes for reloop-0.1.0.tar.gz
Algorithm Hash digest
SHA256 80282200177a00dfd547cbe2e25ee077933966ca5774502c53dcdd91ee9e41d8
MD5 2fac3b38727cdf1801a73bdb50017686
BLAKE2b-256 77fd67e889a04bb21fa2edb1eca7b0c968c7576d3592310076304457e19e2467

See more details on using hashes here.

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

Hashes for reloop-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1010afc0fc7864663f55a191584b1f54bb3b56579dddefd8fd4236d000a2a49f
MD5 ea6915341e007c96664539349f41bbf9
BLAKE2b-256 73f8beea4cf9b82344832aee64b053063f3a9f7647060176be2e069192f04145

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page