python
runtime_error
ai_generated
true
RuntimeError: no running event loop
ID: python/no-running-event-loop
80%Fix Rate
89%Confidence
0Evidence
2024-01-22First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.8 | active | — | — | — |
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
| 3.11 | active | — | — | — |
| 3.12 | active | — | — | — |
Root Cause
asyncio.get_running_loop() (or an API that calls it internally, like asyncio.create_task) was invoked from synchronous code outside any running loop.
generic中文
在没有任何正在运行的循环的同步代码中调用了 asyncio.get_running_loop()(或内部调用它的 API,如 asyncio.create_task)。
Workarounds
-
95% success
Only call create_task/get_running_loop from inside a coroutine: async def main(): asyncio.create_task(worker()) asyncio.run(main()) -
85% success
From sync code that must schedule work, use asyncio.run_coroutine_threadsafe when you have a loop reference: future = asyncio.run_coroutine_threadsafe(coro(), loop)
-
80% success
In Jupyter/IPython, use `await` directly in the cell, or call nest_asyncio.apply() once at the top of the notebook.
Dead Ends
Common approaches that don't work:
-
55% fail
Deprecated since 3.10 and emits DeprecationWarning; in 3.12 it still raises when no loop is set.
-
95% fail
Makes it worse — guarantees no loop is available.
-
90% fail
Timing has nothing to do with loop presence.