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

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

## 根因

在没有任何正在运行的循环的同步代码中调用了 asyncio.get_running_loop()（或内部调用它的 API，如 asyncio.create_task）。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   Only call create_task/get_running_loop from inside a coroutine:

async def main():
    asyncio.create_task(worker())

asyncio.run(main())
   ```
2. **** (85% 成功率)
   ```
   From sync code that must schedule work, use asyncio.run_coroutine_threadsafe when you have a loop reference:

future = asyncio.run_coroutine_threadsafe(coro(), loop)
   ```
3. **** (80% 成功率)
   ```
   In Jupyter/IPython, use `await` directly in the cell, or call nest_asyncio.apply() once at the top of the notebook.
   ```

## 无效尝试

- **** — Deprecated since 3.10 and emits DeprecationWarning; in 3.12 it still raises when no loop is set. (55% 失败率)
- **** — Makes it worse — guarantees no loop is available. (95% 失败率)
- **** — Timing has nothing to do with loop presence. (90% 失败率)
