RuntimePython Error

RuntimeError

RuntimeError: This event loop is already running

Traceback

terminal
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 running

What 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

Broken
import asyncio

async def inner():
    return 42

async def outer():
    result = asyncio.run(inner())  # loop already running!

Fixed code

Fixed
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

1

Infinite recursion from missing or incorrect base cases

2

Modifying dictionaries or sets during iteration

3

Calling generators or coroutines in unsupported ways

4

Using asyncio event loops incorrectly or attempting to nest them

Related errors