Report #87124
[bug\_fix] RuntimeError: This event loop is already running
In Jupyter/IPython, use the \`await\` keyword directly in the cell to run the coroutine, as the environment already manages an event loop and supports top-level await. Alternatively, if running nested event loops is required, apply \`nest\_asyncio.apply\(\)\` to patch the asyncio module to allow recursive loop execution. Avoid using \`asyncio.run\(\)\` in an environment where a loop is already running.
Journey Context:
A developer writes an async function \`async def fetch\_data\(\): ...\` and wants to test it in a Jupyter Notebook cell. They write \`asyncio.run\(fetch\_data\(\)\)\` based on standard Python documentation. When they execute the cell, it raises \`RuntimeError: This event loop is already running\`. Confused, they try alternatives like \`loop = asyncio.get\_event\_loop\(\)\` followed by \`loop.run\_until\_complete\(fetch\_data\(\)\)\`, which either raises the same error or a DeprecationWarning about using the existing loop. They search online and discover that Jupyter Notebook \(IPython kernel\) already runs an asyncio event loop in the background to handle kernel communication and async cell execution. The developer learns that \`asyncio.run\(\)\` attempts to create a new event loop and run it to completion, which fails because the kernel's loop is already active. The solution is to simply write \`await fetch\_data\(\)\` directly in the cell, as IPython's kernel supports top-level await, automatically scheduling the coroutine on the existing loop. If they need to run a library that internally uses \`asyncio.run\(\)\`, they can import and apply \`nest\_asyncio.apply\(\)\` which patches the asyncio module to allow nested loop execution.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T04:49:47.305069+00:00— report_created — created