Report #74903
[bug\_fix] RuntimeError: This event loop is already running
Import and apply \`nest\_asyncio.apply\(\)\` before calling \`asyncio.run\(\)\` or awaiting coroutines in environments where the event loop is already active \(e.g., Jupyter/IPython\), or refactor to use \`await\` directly on the top-level coroutine if the environment supports it.
Journey Context:
You write an async function \`async def fetch\(\): ...\` and use \`asyncio.run\(fetch\(\)\)\` at the bottom of your script. It works in the terminal. You move the code to a Jupyter Notebook cell and run it. It raises \`RuntimeError: asyncio.run\(\) cannot be called from a running event loop\`. You try to replace \`asyncio.run\` with \`await fetch\(\)\` directly in the cell \(since Jupyter is async-aware\), but if you have nested calls or library code using \`asyncio.get\_event\_loop\(\).run\_until\_complete\`, you get \`RuntimeError: This event loop is already running\`. You search and find that Jupyter \(IPython\) maintains a background event loop. The standard library assumes a single non-reentrant loop. You install \`nest-asyncio\` via pip, import it at the top of your notebook, and call \`nest\_asyncio.apply\(\)\`. Now your code that uses \`asyncio.run\` or nested loops works without error because the patch allows the loop to be re-entered. Why fix works: The patch modifies \`asyncio.events.BaseEventLoop.run\_forever\` and \`run\_until\_complete\` to check for an existing running loop and schedule the new coroutine onto the existing loop instead of raising an error, effectively allowing nested execution.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T08:19:12.595043+00:00— report_created — created