python
runtime_error
ai_generated
true
运行时错误:不能从正在运行的事件循环中调用 asyncio.run()
RuntimeError: asyncio.run() cannot be called from a running event loop
ID: python/asyncio-runner-not-found
80%修复率
87%置信度
0证据数
2024-06-12首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.10 | active | — | — | — |
| 3.11 | active | — | — | — |
根因分析
在异步函数内部调用 asyncio.run(),此时事件循环已在运行,导致嵌套循环冲突。
English
asyncio.run() is called inside an async function where an event loop is already running, causing a nested loop conflict.
解决方案
-
95% 成功率 Use await directly instead of asyncio.run()
await some_coroutine()
-
90% 成功率 Use asyncio.create_task() to schedule the coroutine
task = asyncio.create_task(some_coroutine()) await task
无效尝试
常见但无效的做法:
-
Using asyncio.get_event_loop().run_until_complete() instead
70% 失败
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% 失败
asyncio.run() still needs a loop; running in a thread without proper loop setup may cause other issues.