# RuntimeError: 没有正在运行的事件循环

- **ID:** `python/asyncio-no-running-event-loop`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在活动事件循环之外调用了 asyncio.get_running_loop() 或执行了 await，通常来自同步代码或未进入 asyncio.run() 的线程。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8+ | active | — | — |
| 3.9+ | active | — | — |
| 3.10+ | active | — | — |
| 3.11+ | active | — | — |
| 3.12+ | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   async def main():
    ...

if __name__ == '__main__':
    asyncio.run(main())
   ```
2. **** (90% 成功率)
   ```
   loop = main_loop  # captured earlier
future = asyncio.run_coroutine_threadsafe(coro(), loop)
result = future.result(timeout=10)
   ```

## 无效尝试

- **** — In Python 3.10+ this raises DeprecationWarning and in 3.12 raises RuntimeError when no loop exists. (70% 失败率)
- **** — No loop will spontaneously appear; the coroutine never runs. (90% 失败率)
