debugModeratepending
Python asyncio event loop already running -- Jupyter and nested async
Viewed 0 times
event loop already runningasyncio.runnest_asyncioJupyterRuntimeError
pythonjupyter
Error Messages
Problem
Calling asyncio.run() inside Jupyter notebook or any environment with an already-running event loop throws RuntimeError. Cannot use standard async patterns in interactive environments.
Solution
In Jupyter: use await directly (top-level await works in IPython 7+). Or use nest_asyncio: import nest_asyncio; nest_asyncio.apply() to allow nested event loops. In scripts: ensure only one asyncio.run() at the top level. In libraries: never call asyncio.run() -- accept and return coroutines, let the caller manage the loop.
Why
asyncio.run() creates a new event loop and runs until complete. If a loop is already running (Jupyter, some web frameworks), it cannot create another one in the same thread.
Revisions (0)
No revisions yet.