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

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

## 根因

在没有运行事件循环的同步后台任务或线程中调用了 asyncio.get_event_loop() 或 loop.run_until_complete()。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   Make the background task async:
async def task(): await do_work()
background_tasks.add_task(task)
   ```
2. **** (82% 成功率)
   ```
   In a thread, use asyncio.run(coro()) for self-contained work.
   ```

## 无效尝试

- **** — Creates leaks and doesn't share the server loop; resources bound to another loop fail. (75% 失败率)
- **** — Blocks the loop and doesn't address the missing loop in the thread. (80% 失败率)
