# 运行时错误：任务获取了附加到不同事件循环的 Future

- **ID:** `python/fastapi-sync-endpoint-blocking`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在 FastAPI 的同步端点中使用了异步库或跨线程操作，导致事件循环冲突。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (85% 成功率)
   ```
   from starlette.concurrency import run_in_threadpool; result = await run_in_threadpool(sync_func)
   ```
2. **** (95% 成功率)
   ```
   async def endpoint(): await async_func()
   ```

## 无效尝试

- **** — 如果库是同步的，会阻塞事件循环，但不会导致此错误。 (30% 失败率)
- **** — 线程和事件循环不兼容，导致 Future 跨循环。 (90% 失败率)
