Report #64653
[bug\_fix] RuntimeError: asyncio.run\(\) cannot be called from a running event loop
In environments that already have a running event loop \(such as Jupyter Notebook, IPython, or an async web framework\), \`await\` the coroutine directly \(e.g., \`await main\(\)\`\) instead of using \`asyncio.run\(\)\`. Alternatively, use \`nest\_asyncio\` to allow nested loops \(not recommended for production\).
Journey Context:
A developer writes a robust async data processing script \`main.py\` with an async \`main\(\)\` entry point. Following best practices, they include \`if \_\_name\_\_ == "\_\_main\_\_": asyncio.run\(main\(\)\)\` at the bottom. The script works flawlessly when executed from the command line. Later, they want to demonstrate it in a Jupyter notebook for stakeholders. They import the \`main\` function and call \`asyncio.run\(main\(\)\)\` in a cell. Immediately, a \`RuntimeError\` is raised stating that \`asyncio.run\(\)\` cannot be called from a running event loop. Confused, they search and discover that Jupyter kernels run on top of an asyncio event loop to handle cell execution asynchronously. They realize \`asyncio.run\(\)\` is designed to be the entry point for synchronous contexts, managing the lifecycle of a new loop. Instead, in the notebook, they simply call \`await main\(\)\`, and the coroutine executes successfully within the existing loop.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T15:00:15.634121+00:00— report_created — created