← All fixes
RuntimeError: asyncio.run() cannot be called from a running event loop
#160Error
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 yet0 worked0 didn't
Tried it yourself?
Related fixes
Crash: Unhandled 'error' event from a stream or EventEmitter
#33node:events: throw er; // Unhandled 'error' event. Error: ENOENT: no such file or directory, open 'missing.txt'. Emitted 'error' event on ReadStream instance
nodeVerifiedNo reports yet
RangeError: Maximum call stack size exceeded (recursion with no base case)
#55RangeError: Maximum call stack size exceeded
nodejavascriptVerifiedNo reports yet
RuntimeError: dictionary changed size during iteration
#49RuntimeError: dictionary changed size during iteration
pythonVerifiedNo reports yet