# 运行时错误：事件循环已关闭：无法调用 call_soon()

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

## 根因

在已关闭的事件循环上调度回调，通常在 asyncio.run() 完成后发生。

## 版本兼容性

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

## 解决方案

1. **Ensure all callbacks are scheduled before the loop closes** (85% 成功率)
   ```
   loop = asyncio.get_running_loop()
loop.call_soon(some_callback)
# ensure loop is still running
   ```
2. **Use asyncio.get_running_loop() to check if loop is active** (90% 成功率)
   ```
   try:
    loop = asyncio.get_running_loop()
    loop.call_soon(callback)
except RuntimeError:
    # loop not running, handle accordingly
   ```

## 无效尝试

- **Using asyncio.get_event_loop() to get a new loop** — If the loop is closed, get_event_loop() may raise an error or return a closed loop. (60% 失败率)
- **Ignoring the error and assuming the callback ran** — The callback never executes, leading to incomplete operations. (50% 失败率)
