Report #17291
[bug\_fix] RuntimeError: This event loop is already running when calling asyncio.run\(\) in Jupyter Notebook
In Jupyter/IPython \(7.0\+\), use \`await coro\(\)\` directly in the cell instead of \`asyncio.run\(coro\(\)\)\`, as the kernel already runs an event loop and supports top-level await. If using library code that internally calls \`asyncio.run\`, install and apply \`nest\_asyncio\` \(\`import nest\_asyncio; nest\_asyncio.apply\(\)\`\) to allow nested loops.
Journey Context:
You write an async function \`async def fetch\_data\(\): ...\` and try to run it in a Jupyter notebook cell with \`asyncio.run\(fetch\_data\(\)\)\`. The cell immediately raises \`RuntimeError: asyncio.run\(\) cannot be called from a running event loop\`. This occurs because the IPython kernel \(since version 7\) maintains its own asyncio event loop running in the background to handle kernel messages and execute async code. \`asyncio.run\(\)\` attempts to create a new event loop and set it as the current thread's loop, which is forbidden when a loop is already running. The fix of using \`await fetch\_data\(\)\` works because Jupyter patches the event loop policy to allow coroutines to be awaited directly at the top level of cells, running them in the existing loop. \`nest\_asyncio\` patches the loop to allow nested execution, effectively making \`asyncio.run\(\)\` reuse the existing loop instead of trying to create a new one.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T04:55:43.138184+00:00— report_created — created