# RuntimeError: 无法从正在运行的事件循环中调用 asyncio.run()

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

## 根因

在已运行的事件循环内部调用 asyncio.run()，例如在 Jupyter 笔记本单元格中或异步函数内部调用。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   # in a Jupyter cell
result = await main()  # top-level await, no asyncio.run
   ```
2. **** (85% 成功率)
   ```
   import threading, asyncio
result = {}
def _run():
    result['v'] = asyncio.run(main())
t = threading.Thread(target=_run); t.start(); t.join()
   ```

## 无效尝试

- **** — Raises the same error because the loop is already running. (90% 失败率)
- **** — Works only if you create a fresh loop in that thread; otherwise you get 'no running event loop'. (70% 失败率)
