{"id":541,"library":"nest-asyncio","title":"nest-asyncio Library","description":"nest-asyncio is a Python library that patches the `asyncio` module to allow nested event loops. This addresses the `RuntimeError: This event loop is already running` issue encountered in environments like web servers, GUI applications, and Jupyter notebooks where an event loop is already active. It is currently at version 1.6.0 and maintains a stable release cadence.","status":"active","version":"1.6.0","language":"python","source_language":"en","source_url":"https://github.com/erdewit/nest_asyncio","tags":["asyncio","event loop","patching","nested","jupyter","web servers","gui"],"install":[{"cmd":"pip install nest-asyncio","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"note":"The `apply()` function is typically called once at the start of your application to patch the current or specified event loop.","symbol":"apply","correct":"import nest_asyncio\nnest_asyncio.apply()"}],"quickstart":{"code":"import asyncio\nimport nest_asyncio\n\n# Apply the patch to allow nested event loops\nnest_asyncio.apply()\n\nasync def my_async_function():\n    await asyncio.sleep(0.1)\n    return \"Hello, Nested World!\"\n\n# This works even in environments with existing event loops\nresult = asyncio.run(my_async_function())\nprint(result)","lang":"python","description":"This quickstart demonstrates how to apply the `nest-asyncio` patch and then run an `asyncio` coroutine using `asyncio.run()`, even if an event loop is already running."},"warnings":[{"fix":"On Python 3.14+, use `await` with async methods (e.g., `await obj.async_method()`) instead of relying on `nest-asyncio`'s patching for automatic conversion.","message":"Python 3.14+ breaks the `nest-asyncio` workaround. Due to significant changes in `asyncio`'s implementation, the library can no longer safely monkey-patch it, meaning automatic async-to-sync conversion often fails. Users must explicitly use `await` with async methods in environments like Jupyter notebooks.","severity":"breaking","affected_versions":">=3.14"},{"fix":"While often ignorable as the loop is reused, be aware of this warning. For critical applications, ensure proper event loop management, or consider explicit loop closing if not relying on reuse (though this often defeats `nest-asyncio`'s purpose).","message":"Using `nest-asyncio` can lead to a `ResourceWarning: unclosed event loop at exit` on Python 3.12+ if the patched `asyncio.run()` creates a new event loop and doesn't explicitly close it. This warning is often hidden by default.","severity":"gotcha","affected_versions":">=3.12"},{"fix":"Ensure you are using `asyncio`'s default event loop when `nest-asyncio` is applied. If you require other loop implementations, `nest-asyncio` may not be the solution.","message":"`nest-asyncio` is designed to patch `asyncio` event loops only. It generally does not provide compatibility or patching for alternative event loop implementations like `uvloop` or `quamash`.","severity":"gotcha","affected_versions":"All"},{"fix":"Avoid long-running or CPU-bound operations within nested `asyncio.run()` calls. If significant work is needed, consider restructuring your async code to avoid deep nesting or using thread pools for blocking operations.","message":"Nesting `asyncio.run()` calls after applying `nest-asyncio` can potentially lead to 'starvation' of tasks scheduled outside the nested run if the inner loop occupies too much execution time. This happens because nested runs do not automatically yield time to outer tasks.","severity":"gotcha","affected_versions":"All"},{"fix":"When working with frameworks that have their own async integration mechanisms, prefer using their recommended patterns over `nest-asyncio` to ensure compatibility and stability.","message":"Some frameworks, like Prefect, advise against using `nest-asyncio` and provide their own async utilities (e.g., `run_coro_as_sync`) to bridge async and sync code, explicitly warning that `nest-asyncio` can cause hard-to-debug issues like deadlocks, broken cancellations, and inconsistent behavior.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-05-12T14:50:00.109Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Apply `nest-asyncio` at the beginning of your script or notebook to patch `asyncio` and allow nested event loops:\n```python\nimport nest_asyncio\nnest_asyncio.apply()\n\n# Now you can run async code even if a loop is already running\nimport asyncio\n\nasync def main():\n    print('Hello from async!')\n\nasyncio.run(main())\n```","cause":"The `asyncio` module by design does not allow an event loop to be nested, meaning you cannot call `asyncio.run()` or `loop.run_until_complete()` if an event loop is already active, which commonly occurs in environments like Jupyter notebooks, web servers, or GUI applications.","error":"RuntimeError: This event loop is already running"},{"fix":"Import and apply `nest_asyncio` at the start of your program or notebook cell to enable re-entrant use of `asyncio.run()`:\n```python\nimport nest_asyncio\nnest_asyncio.apply()\n\nimport asyncio\n\nasync def my_async_function():\n    await asyncio.sleep(0.1)\n    return 'Done!'\n\n# This will now work without error in environments with an existing loop\nresult = asyncio.run(my_async_function())\nprint(result)\n```","cause":"This error is a specific manifestation of the 'event loop already running' problem, occurring when `asyncio.run()` is invoked inside a context where the main event loop is already active, preventing the creation of a new top-level loop.","error":"RuntimeError: asyncio.run() cannot be called from a running event loop"},{"fix":"Install the library using pip:\n```bash\npip install nest-asyncio\n```\nOr, if using Anaconda/Conda:\n```bash\nconda install -c conda-forge nest-asyncio\n```","cause":"The `nest_asyncio` library is not installed in the Python environment currently being used, or the environment where it was installed is not active.","error":"ModuleNotFoundError: No module named 'nest_asyncio'"},{"fix":"Upgrade your Python interpreter to version 3.7 or higher. If upgrading is not an option, you must use the older `asyncio` loop management methods (e.g., `asyncio.get_event_loop().run_until_complete(coroutine)`) and apply `nest_asyncio` to the loop:\n```python\nimport asyncio\nimport nest_asyncio\n\nasync def old_style_async():\n    print(\"Running in old-style loop\")\n\nloop = asyncio.get_event_loop()\nnest_asyncio.apply(loop)\nloop.run_until_complete(old_style_async())\n```","cause":"The `asyncio.run()` function was introduced in Python 3.7. This error occurs when attempting to use `asyncio.run()` in a Python version older than 3.7, even if `nest-asyncio` itself supports Python 3.5+.","error":"AttributeError: module 'asyncio' has no attribute 'run'"},{"fix":"Ensure that you are using the default `asyncio` event loop. If you are explicitly setting a third-party loop like `uvloop`, you might need to revert to the default `asyncio` loop for `nest-asyncio` to function correctly, or investigate if the third-party loop offers its own nesting mechanisms or compatibility layers.\n\nExample (if using `uvloop`, remove its explicit installation/setting):\n```python\n# If you were doing something like this, remove or comment it out:\n# import uvloop\n# uvloop.install()\n\nimport nest_asyncio\nnest_asyncio.apply()\n\nimport asyncio\n\nasync def my_async_task():\n    print(\"Running with patched asyncio loop\")\n\nasyncio.run(my_async_task())\n```","cause":"`nest-asyncio` is designed to patch standard `asyncio` event loops. This error occurs when `nest_asyncio.apply()` is called with or attempts to patch a non-standard event loop implementation, such as `uvloop` or `quamash`, which are not directly supported by `nest-asyncio`.","error":"ValueError: Can't patch loop of type <type>"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.12,"mem_mb":4,"disk_size":"17.8M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.09,"mem_mb":4,"disk_size":"18M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.21,"mem_mb":4.9,"disk_size":"19.6M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.2,"mem_mb":4.9,"disk_size":"20M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.5,"mem_mb":8.2,"disk_size":"11.5M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.43,"mem_mb":8.2,"disk_size":"12M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.43,"mem_mb":8.7,"disk_size":"11.1M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.49,"mem_mb":8.7,"disk_size":"12M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.13,"mem_mb":3.9,"disk_size":"17.3M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.11,"mem_mb":3.9,"disk_size":"18M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}