python
runtime_error
ai_generated
true
RuntimeError: Cannot run the event loop while another loop is running
ID: python/asyncio-loop-already-running-nested
80%Fix Rate
87%Confidence
0Evidence
2024-06-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
| 3.11 | active | — | — | — |
| 3.12 | active | — | — | — |
Root Cause
loop.run_until_complete() was called from inside a coroutine that is already executing on the same loop, causing nested loop execution which asyncio forbids.
generic中文
在已运行于同一循环的协程内部调用了 loop.run_until_complete(),导致 asyncio 禁止的嵌套循环执行。
Workarounds
-
95% success
async def outer(): # WRONG: loop.run_until_complete(inner()) await inner() # correct -
88% success
result = await asyncio.to_thread(sync_blocking_fn, arg1, arg2)
Dead Ends
Common approaches that don't work:
-
90% fail
get_event_loop returns the running loop; run_until_complete still raises.
-
85% fail
The loop is still running; a yield point does not allow re-entrant run_until_complete.