Report #9300
[bug\_fix] RuntimeError: asyncio.run\(\) cannot be called from a running event loop
In Jupyter/IPython, use \`await main\(\)\` directly instead of \`asyncio.run\(main\(\)\)\`, or apply \`nest\_asyncio.apply\(\)\` to allow nested event loops.
Journey Context:
Developer writes an async application using \`asyncio.run\(main\(\)\)\` which works in a standalone script. They open a Jupyter Notebook to test the \`main\(\)\` coroutine. When the cell executes \`asyncio.run\(main\(\)\)\`, it raises RuntimeError stating it cannot be called from a running event loop. This occurs because the IPython kernel \(underlying Jupyter\) already runs an event loop to handle asynchronous kernel messages and execution. \`asyncio.run\(\)\` attempts to create a new event loop and set it as the current thread's loop, but detects the existing running loop and aborts. The fix leverages the fact that Jupyter cells can directly await coroutines. By replacing \`asyncio.run\(main\(\)\)\` with \`await main\(\)\`, the coroutine executes within the existing event loop. Alternatively, \`nest\_asyncio\` patches the loop to allow nested execution, enabling \`asyncio.run\(\)\` to work by effectively making the loop reentrant.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T07:47:54.303828+00:00— report_created — created