Report #53225
[bug\_fix] RuntimeError: This event loop is already running in Jupyter
Import and apply nest\_asyncio to allow nested event loops by running 'import nest\_asyncio; nest\_asyncio.apply\(\)' at the start of the notebook, or use 'await' directly at the top level in newer IPython \(7.0\+\) which handles the running loop natively, or refactor to use 'asyncio.get\_running\_loop\(\).create\_task\(\)' instead of 'asyncio.run\(\)'.
Journey Context:
Data scientist writes an async web scraper using aiohttp in a Jupyter notebook. They define an async function 'fetch\(\)' and then call 'asyncio.run\(fetch\(\)\)' in a cell. Immediately, they receive 'RuntimeError: This event loop is already running'. Confused, they try 'await fetch\(\)' directly in the cell. In their older IPython version, this causes a SyntaxError; in a newer version it works for that cell, but when they try to use a library that internally calls 'asyncio.run\(\)', it fails again. They search and discover that the Jupyter IPython kernel maintains an asyncio event loop in the main thread to handle kernel communication, so 'asyncio.run\(\)' which tries to create and close a new loop conflicts with the existing one. The solution is to either use 'nest\_asyncio', a library that patches the asyncio loop to allow nesting, or to use the already-running loop via 'asyncio.get\_running\_loop\(\)' and 'create\_task\(\)'. They install nest\_asyncio, import it, call 'nest\_asyncio.apply\(\)', and their code runs without modification.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T19:50:16.084250+00:00— report_created — created