python
runtime_error
ai_generated
true
运行时错误:线程'Thread-1'中没有当前事件循环
RuntimeError: There is no current event loop in thread 'Thread-1'
ID: python/asyncio-loop-created-in-thread
80%修复率
87%置信度
0证据数
2025-03-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.7 | active | — | — | — |
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
根因分析
在没有运行事件循环的线程中尝试使用asyncio函数。
English
Attempting to use asyncio functions in a thread without a running event loop.
解决方案
-
95% 成功率
loop = asyncio.new_event_loop(); asyncio.set_event_loop(loop); loop.run_until_complete(coro())
-
90% 成功率
future = asyncio.run_coroutine_threadsafe(coro(), loop)
无效尝试
常见但无效的做法:
-
70% 失败
In Python 3.10+, it may raise an error or return a closed loop.
-
50% 失败
asyncio.run() creates a new loop, but it's not accessible from other threads.