{"id":4647,"library":"nest-asyncio2","title":"nest-asyncio2","description":"nest-asyncio2 is a fork of the unmaintained `nest_asyncio` library, designed to patch `asyncio` to allow nested event loops. It addresses compatibility issues with Python 3.12+ and 3.14+ where the original library would fail. The current version is 1.7.2, and it receives updates as needed to maintain compatibility with newer Python versions and fix issues.","status":"active","version":"1.7.2","language":"en","source_language":"en","source_url":"https://github.com/Chaoses-Ib/nest-asyncio2","tags":["asyncio","event loop","patch","concurrency","python3"],"install":[{"cmd":"pip install nest-asyncio2","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"nest-asyncio2 is a direct replacement for the original `nest_asyncio` for Python 3.12+ compatibility. Ensure you use the correct package name.","wrong":"import nest_asyncio\nnest_asyncio.apply()","symbol":"apply","correct":"import nest_asyncio2\nnest_asyncio2.apply()"}],"quickstart":{"code":"import asyncio\nimport nest_asyncio2\n\nasync def inner_coroutine():\n    print(\"Inner coroutine running.\")\n    await asyncio.sleep(0.01)\n    print(\"Inner coroutine finished.\")\n\nasync def outer_coroutine():\n    print(\"Outer coroutine starting inner loop.\")\n    # This would normally fail with RuntimeError: This event loop is already running\n    # but nest_asyncio2 allows it.\n    await asyncio.run(inner_coroutine())\n    print(\"Outer coroutine finished inner loop.\")\n\n# Apply the patch to allow nested event loops\nnest_asyncio2.apply()\n\n# Run the outer coroutine, which itself runs an inner event loop\nprint(\"Running outer event loop.\")\nasyncio.run(outer_coroutine())\nprint(\"Program finished.\")","lang":"python","description":"This example demonstrates how to use `nest_asyncio2.apply()` to enable nested `asyncio.run()` calls. Without the patch, the inner `asyncio.run()` call would raise a `RuntimeError` because an event loop is already running."},"warnings":[{"fix":"Migrate your `nest_asyncio` usage to `nest-asyncio2` by changing `import nest_asyncio` to `import nest_asyncio2` and updating `pip install` commands.","message":"The original `nest_asyncio` library is unmaintained and incompatible with Python 3.12+ (e.g., `asyncio.current_task()` and `loop_factory` issues). `nest-asyncio2` is a fork specifically addressing these incompatibilities.","severity":"breaking","affected_versions":"Python 3.12+ when using `nest_asyncio`"},{"fix":"Ensure that only one of `nest_asyncio` or `nest-asyncio2` is applied. It's recommended to standardize on `nest-asyncio2` for modern Python versions.","message":"`nest-asyncio2.apply()` will warn if `asyncio` is already patched by `nest_asyncio` (on Python 3.12+) or can be configured to raise a `RuntimeError` by setting `error_on_mispatched=True`.","severity":"gotcha","affected_versions":"nest-asyncio2 v1.7.1+ on Python 3.12+"},{"fix":"This is usually a cosmetic warning and doesn't require action. Be aware it might appear in specific environments or with custom warning filters.","message":"On Python 3.12+, patched `asyncio.run()` might create a new event loop if none exists, which could lead to a `ResourceWarning: unclosed event loop` at exit. This warning is often hidden by default.","severity":"gotcha","affected_versions":"nest-asyncio2 on Python 3.12+"},{"fix":"Upgrade to `nest-asyncio2` v1.7.2 or later to ensure `call_soon()` callbacks are executed as expected.","message":"In versions prior to `v1.7.2`, `asyncio.call_soon()` callbacks might not have been properly called during `asyncio.run()` in nested contexts.","severity":"gotcha","affected_versions":"nest-asyncio2 < v1.7.2"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}