python
runtime_error
ai_generated
true
RuntimeError: cannot reuse already awaited coroutine
ID: python/asyncio-cannot-reuse-already-awaited-coroutine
80%Fix Rate
90%Confidence
0Evidence
2024-04-19First 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 object was awaited once and then awaited again. Coroutine objects are single-use; each call to a coroutine function yields a fresh coroutine.
generic中文
协程对象被 await 一次后再次 await。协程对象只能使用一次;每次调用协程函数都会产生新的协程。
Workarounds
-
97% success
# Wrong c = fetch(url) await c await c # Right await fetch(url) await fetch(url)
-
92% success
task = asyncio.create_task(fetch(url)) await task await task # Tasks are awaitable multiple times
Dead Ends
Common approaches that don't work:
-
95% fail
The coroutine state is exhausted; send raises StopIteration immediately.
-
90% fail
The coroutine object is already closed; ensure_future raises the same error.