Report #55112
[bug\_fix] RuntimeError: This event loop is already running in Jupyter/IPython when using asyncio.run
Do not use \`asyncio.run\(\)\` inside Jupyter; instead \`await\` the coroutine directly at the top level of the cell \(supported in IPython 7\+\). If calling async code from sync code within the notebook \(e.g., a callback\), install and apply \`nest\_asyncio\`: \`import nest\_asyncio; nest\_asyncio.apply\(\)\`. This patches the running loop to allow nested execution.
Journey Context:
Developer writes an async scraping function \`async def fetch\(url\): ...\`. In a Jupyter cell, they write \`asyncio.run\(fetch\("http://example.com"\)\)\`. Executing the cell raises \`RuntimeError: asyncio.run\(\) cannot be called from a running event loop\`. Developer is confused because the code works in a \`.py\` script. They learn that Jupyter kernels run on Tornado, which already has an event loop in the main thread. They try \`await fetch\(...\)\` directly in the cell, which works and returns the result. However, later they need to call this from inside a synchronous library method \(e.g., a pandas accessor\). They try \`asyncio.get\_event\_loop\(\).run\_until\_complete\(...\)\`, same error. They find \`nest\_asyncio\` via StackOverflow, install it, add \`nest\_asyncio.apply\(\)\` at the top of the notebook, and now \`asyncio.run\(\)\` works inside the notebook by allowing nested loops.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T22:59:58.419293+00:00— report_created — created