# 运行时错误：任务 <Task pending> 获取了附加到不同循环的 Future <Future pending>

- **ID:** `python/asyncio-future-attached-to-loop`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

协程或任务等待了一个属于不同事件循环的 Future，通常是由于跨线程混合循环或在 asyncio.gather 中使用来自不同循环的任务。

## 版本兼容性

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

## 解决方案

1. **Ensure all coroutines and futures are created in the same event loop** (90% 成功率)
   ```
   loop = asyncio.new_event_loop(); asyncio.set_event_loop(loop); loop.run_until_complete(main())
   ```
2. **Use asyncio.run() to avoid loop conflicts entirely** (95% 成功率)
   ```
   async def main(): await asyncio.gather(coro1(), coro2()); asyncio.run(main())
   ```

## 无效尝试

- **Creating a new loop and setting it globally** — The Future is still attached to the original loop; global setting does not migrate it. (60% 失败率)
- **Using asyncio.ensure_future instead of create_task** — ensure_future also attaches to the current loop; same issue. (70% 失败率)
