python
runtime_error
ai_generated
true
RuntimeError: Task got Future attached to a different loop
ID: python/asyncio-task-got-future-different-loop
80%Fix Rate
88%Confidence
0Evidence
2024-03-27First 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
A coroutine or Future created under one event loop was awaited on another loop, e.g. by calling asyncio.run() twice or mixing loop.run_until_complete() with asyncio.run().
generic中文
在某个事件循环下创建的协程或 Future 被另一个循环 await,例如两次调用 asyncio.run() 或混用 loop.run_until_complete() 与 asyncio.run()。
Workarounds
-
95% success
loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.run_until_complete(main()) # single entry point
-
90% success
# In Jupyter/IPython or uvicorn, just 'await main()' # Do NOT call asyncio.run() inside an already-running loop
Dead Ends
Common approaches that don't work:
-
85% fail
The Future object still references the original loop; identity mismatch remains.
-
80% fail
ensure_future still binds to the current loop, not the Future's original loop.