Report #12007
[bug\_fix] RuntimeError: This event loop is already running \(or asyncio.run\(\) cannot be called from a running event loop\)
In Jupyter/IPython, use \`await func\(\)\` directly in the cell instead of \`asyncio.run\(func\(\)\)\`, or install and apply \`nest\_asyncio\` by running \`import nest\_asyncio; nest\_asyncio.apply\(\)\` at the notebook start to allow nested loops.
Journey Context:
A developer writes an async API client using \`aiohttp\` with \`async def fetch\(\): ...\`. To test it in a Jupyter notebook, they write a cell containing \`import asyncio; asyncio.run\(fetch\(\)\)\`. Upon execution, Jupyter raises \`RuntimeError: asyncio.run\(\) cannot be called from a running event loop\`. This happens because the Jupyter kernel itself runs on an event loop \(Tornado or asyncio\) to handle cell execution and I/O callbacks. \`asyncio.run\(\)\` attempts to create a new loop and set it as the current thread's loop, which is forbidden when a loop is already running. The developer tries \`loop = asyncio.get\_event\_loop\(\); loop.run\_until\_complete\(fetch\(\)\)\`, which sometimes works but triggers deprecation warnings or \`RuntimeError\` in newer Python versions if the loop is already running. The solution is to recognize that Jupyter cells already support top-level \`await\`. Simply writing \`await fetch\(\)\` in the cell works because IPython patches the event loop to accept \`await\` at the top level. For complex cases involving libraries that internally use \`asyncio.run\` \(like \`pytest-asyncio\` or \`uvicorn\`\), applying \`nest\_asyncio\` patches the asyncio loop to allow nesting, resolving the conflict.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T14:50:17.612667+00:00— report_created — created