python
runtime_error
ai_generated
true
RuntimeError: no running event loop
ID: python/fastapi-background-task-event-loop
80%Fix Rate
83%Confidence
0Evidence
2025-01-08First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
asyncio.get_event_loop() or loop.run_until_complete() is called inside a sync background task or thread where no loop is running.
generic中文
在没有运行事件循环的同步后台任务或线程中调用了 asyncio.get_event_loop() 或 loop.run_until_complete()。
Workarounds
-
90% success
Make the background task async: async def task(): await do_work() background_tasks.add_task(task)
-
82% success
In a thread, use asyncio.run(coro()) for self-contained work.
Dead Ends
Common approaches that don't work:
-
75% fail
Creates leaks and doesn't share the server loop; resources bound to another loop fail.
-
80% fail
Blocks the loop and doesn't address the missing loop in the thread.