RuntimeError
RuntimeError: This event loop is already running
Traceback
Traceback (most recent call last):
File "main.py", line 7, in <module>
result = asyncio.run(inner()) # loop already running!
RuntimeError: This event loop is already runningWhat causes this error
asyncio.run() or run_until_complete() was called from within an already-running event loop. Event loops cannot be nested in standard asyncio.
How to fix it
Use `await` instead of `asyncio.run()` when inside an async context. In Jupyter, use `await` directly or install `nest_asyncio`. Restructure to avoid running sync wrappers from async code.
Code that causes this error
import asyncio
async def inner():
return 42
async def outer():
result = asyncio.run(inner()) # loop already running!Fixed code
import asyncio
async def inner():
return 42
async def outer():
result = await inner() # just await it
return result
asyncio.run(outer())About RuntimeError
This RuntimeError occurs when `asyncio.run()` or `loop.run_until_complete()` is called from within an already-running event loop. This is common in Jupyter notebooks (which run their own event loop), in web frameworks like FastAPI/Starlette that manage their own loop, and when trying to call synchronous wrappers of async code from within async contexts. The event loop is the core of Python's async model — it runs one piece of code at a time and switches between tasks cooperatively.
Nesting event loops is not supported by default. The `nest_asyncio` library patches the event loop to allow nesting (useful in Jupyter), but the proper solution is to restructure code to use `await` within the running loop instead of trying to start a new one. Understanding the boundary between sync and async code is key to avoiding this error.
Common scenarios
Infinite recursion from missing or incorrect base cases
Modifying dictionaries or sets during iteration
Calling generators or coroutines in unsupported ways
Using asyncio event loops incorrectly or attempting to nest them