Report #7096
[bug\_fix] RuntimeError: This event loop is already running when calling asyncio.run\(\) inside Jupyter/IPython
Use 'nest\_asyncio.apply\(\)' at the start of the notebook to allow nested event loops, or use 'await' directly in the notebook cell \(since Jupyter already has a running loop\), or run the async code in a separate thread using 'asyncio.run\_coroutine\_threadsafe'.
Journey Context:
Developer is working in a Jupyter notebook. They have an async function 'async def fetch\_data\(\): ...'. They try to test it by running 'asyncio.run\(fetch\_data\(\)\)' in a cell and immediately get 'RuntimeError: This event loop is already running'. They try 'await fetch\_data\(\)' and it works in that cell, but they need to call it from within a synchronous function inside the notebook. They search and learn that Jupyter kernels run an asyncio event loop in the main thread to handle async kernel operations. The 'asyncio.run\(\)' function tries to create a new event loop and run it, which is forbidden when a loop is already running. The solution is to install 'nest-asyncio' \('pip install nest-asyncio'\) and run 'import nest\_asyncio; nest\_asyncio.apply\(\)' at the top of the notebook. This patches the asyncio loop to allow nested execution, making 'asyncio.run\(\)' work as expected.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T01:46:41.198843+00:00— report_created — created