Report #90143
[bug\_fix] RuntimeError: asyncio.run\(\) cannot be called from a running event loop
In Jupyter Notebook or IPython, use 'await main\(\)' directly in the cell instead of 'asyncio.run\(main\(\)\)', because the kernel already runs an event loop. If you need to run async code from a sync function inside Jupyter, install and apply 'nest\_asyncio': 'import nest\_asyncio; nest\_asyncio.apply\(\)'.
Journey Context:
Developer writes an async script 'async def fetch\(\): ...' and 'asyncio.run\(fetch\(\)\)' in a standalone .py file. It works. They then copy the code into a Jupyter Notebook cell for interactive testing. Running the cell raises 'RuntimeError: asyncio.run\(\) cannot be called from a running event loop'. They search and find that Jupyter's IPython kernel already has an event loop running to handle kernel messaging. 'asyncio.run\(\)' tries to create a new loop and set it as the current loop, but detects one is already running in the thread. The fix is to use 'await fetch\(\)' directly in the notebook cell, which works because the cell execution is already async-aware. If the developer is calling async code from within a sync library callback inside the notebook, they use 'nest\_asyncio' to patch the loop to allow nesting.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T09:54:04.603350+00:00— report_created — created