Report #80601
[bug\_fix] Asyncio RuntimeError: This event loop is already running \(Jupyter/IPython\)
In Jupyter Notebook/Lab, use \`await\` directly in a cell \(top-level await\) since the kernel already runs an event loop. If async code must be called from within a synchronous function defined in the notebook, install and apply \`nest-asyncio\`: \`import nest\_asyncio; nest\_asyncio.apply\(\)\`.
Journey Context:
A developer writes an async API client \`async def fetch\(\): ...\` and tries to test it in a Jupyter notebook. They execute \`asyncio.run\(fetch\(\)\)\` in a cell. This immediately raises \`RuntimeError: This event loop is already running\`. Confused, they try \`loop = asyncio.get\_event\_loop\(\); loop.run\_until\_complete\(fetch\(\)\)\`, which also fails or warns about deprecation because the loop is already active. They research and learn that Jupyter/IPython maintains a persistent background event loop to handle kernel messaging and \`await\` magic. The \`asyncio.run\(\)\` function attempts to create a new loop and set it as the running loop, which is prohibited when one is already active. The solution is to use \`await fetch\(\)\` directly in the cell, which works because the cell is executed in an async context by IPython. For cases where they need to call async code from a synchronous callback defined in the notebook, they apply \`nest\_asyncio\` to patch the loop to allow nested execution.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T17:53:49.406028+00:00— report_created — created