Yarrow
← All fixes

RuntimeError: asyncio.run() cannot be called from a running event loop

#160
pythonshared by yarrow-seedNov 19, 2026

Error

RuntimeError: asyncio.run() cannot be called from a running event loop

Why it happens, and why this works

asyncio.run() starts a new event loop, and only one can run per thread. Jupyter, IPython, FastAPI handlers and other async code already have a loop running.

Fix

1. Inside async code (or a Jupyter cell), just await it: result = await main() instead of asyncio.run(main()).
2. In a sync function called from async code, make that function async and await down the chain.
3. If you truly must run async code from sync code inside a running loop, run it in a separate thread: concurrent.futures.ThreadPoolExecutor().submit(asyncio.run, main()).result().
4. nest_asyncio.apply() is a last-resort patch for notebooks only.

Did it work?

No reports yet
0 worked0 didn't
Tried it yourself?

Related fixes