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.
pip install nest-asyncioVerified import paths — ran on the pinned version, not inferred.
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.
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.
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).
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.
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.
When working with frameworks that have their own async integration mechanisms, prefer using their recommended patterns over `nest-asyncio` to ensure compatibility and stability.
Apply `nest-asyncio` at the beginning of your script or notebook to patch `asyncio` and allow nested event loops:
```python
import nest_asyncio
nest_asyncio.apply()
# Now you can run async code even if a loop is already running
import asyncio
async def main():
print('Hello from async!')
asyncio.run(main())
```Import and apply `nest_asyncio` at the start of your program or notebook cell to enable re-entrant use of `asyncio.run()`:
```python
import nest_asyncio
nest_asyncio.apply()
import asyncio
async def my_async_function():
await asyncio.sleep(0.1)
return 'Done!'
# This will now work without error in environments with an existing loop
result = asyncio.run(my_async_function())
print(result)
```Install the library using pip: ```bash pip install nest-asyncio ``` Or, if using Anaconda/Conda: ```bash conda install -c conda-forge nest-asyncio ```
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:
```python
import asyncio
import nest_asyncio
async def old_style_async():
print("Running in old-style loop")
loop = asyncio.get_event_loop()
nest_asyncio.apply(loop)
loop.run_until_complete(old_style_async())
```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.
Example (if using `uvloop`, remove its explicit installation/setting):
```python
# If you were doing something like this, remove or comment it out:
# import uvloop
# uvloop.install()
import nest_asyncio
nest_asyncio.apply()
import asyncio
async def my_async_task():
print("Running with patched asyncio loop")
asyncio.run(my_async_task())
```No dependency data recorded yet.