Report #41334
[bug\_fix] RuntimeError: This event loop is already running or RuntimeError: Cannot run the event loop while another loop is running
Use \`nest\_asyncio.apply\(\)\` from the \`nest-asyncio\` package to allow nested event loops, or use \`await\` directly in Jupyter cells \(which run in an existing loop\) instead of \`asyncio.run\(\)\`. Alternatively, use \`asyncio.run\(\)\` only in standalone scripts, not in interactive environments.
Journey Context:
The developer writes an async function using \`asyncio\` and tries to run it in a Jupyter Notebook cell. They write \`asyncio.run\(my\_coroutine\(\)\)\` and get a RuntimeError saying the event loop is already running. Confused, they try to get the running loop with \`asyncio.get\_running\_loop\(\)\` and see that Jupyter/IPython already has an event loop running for the kernel. They try to close it and create a new one with \`asyncio.new\_event\_loop\(\)\`, but this breaks the kernel's async handling and causes further errors. Searching for solutions, they find that standard asyncio doesn't support nested loops by design. The developer installs \`nest-asyncio\` \(\`pip install nest-asyncio\`\) and calls \`nest\_asyncio.apply\(\)\` at the top of their notebook. This patches the asyncio loop to allow nesting. Now \`asyncio.run\(\)\` works inside the existing Jupyter loop. Alternatively, they learn that in Jupyter they can simply \`await my\_coroutine\(\)\` directly in a cell without asyncio.run\(\), since the cell itself runs inside the existing event loop, making the explicit run call unnecessary and problematic.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T23:51:11.773737+00:00— report_created — created