python
runtime_error
ai_generated
true
RuntimeError: 无法从正在运行的事件循环中调用 asyncio.run()
RuntimeError: asyncio.run() cannot be called from a running event loop
ID: python/asyncio-loop-already-running-jupyter
80%修复率
89%置信度
0证据数
2024-01-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.8+ | active | — | — | — |
| 3.9+ | active | — | — | — |
| 3.10+ | active | — | — | — |
| 3.11+ | active | — | — | — |
| 3.12+ | active | — | — | — |
根因分析
在已运行的事件循环内部调用了 asyncio.run(),常见于 Jupyter notebook、IPython 或异步框架中。
English
asyncio.run() was called from inside an already-running loop, common in Jupyter notebooks, IPython, or async frameworks.
解决方案
-
97% 成功率
# In a Jupyter cell: await main() # Do NOT use asyncio.run() in Jupyter
-
95% 成功率
task = asyncio.create_task(main()) await task
无效尝试
常见但无效的做法:
-
55% 失败
Nest_asyncio patches the loop but breaks some libraries and can deadlock; often produces subtle bugs.
-
90% 失败
Raises the same error because the loop is already running.