python
runtime_error
ai_generated
true
RuntimeError: asyncio.run() cannot be called from a running event loop
ID: python/asyncio-runner-not-found
80%Fix Rate
87%Confidence
0Evidence
2024-06-12First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.10 | active | — | — | — |
| 3.11 | active | — | — | — |
Root Cause
asyncio.run() is called inside an async function where an event loop is already running, causing a nested loop conflict.
generic中文
在异步函数内部调用 asyncio.run(),此时事件循环已在运行,导致嵌套循环冲突。
Workarounds
-
95% success Use await directly instead of asyncio.run()
await some_coroutine()
-
90% success Use asyncio.create_task() to schedule the coroutine
task = asyncio.create_task(some_coroutine()) await task
Dead Ends
Common approaches that don't work:
-
Using asyncio.get_event_loop().run_until_complete() instead
70% fail
This still tries to run a new loop inside an existing one, leading to the same error.
-
Wrapping the call in a thread to avoid loop conflict
50% fail
asyncio.run() still needs a loop; running in a thread without proper loop setup may cause other issues.