python runtime_error ai_generated true

RuntimeError: cannot reuse already awaited coroutine

ID: python/asyncio-run-coroutine-twice

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-12-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.7 active
3.8 active

Root Cause

Attempting to await the same coroutine object more than once, which is not allowed in asyncio.

generic

中文

尝试多次等待同一个协程对象,这在asyncio中是不允许的。

Workarounds

  1. 100% success
    async def my_coro(): pass; await my_coro(); await my_coro()
  2. 95% success
    task = asyncio.create_task(my_coro()); await task; task2 = asyncio.create_task(my_coro()); await task2

Dead Ends

Common approaches that don't work:

  1. 100% fail

    The coroutine is already exhausted; you must create a new coroutine.

  2. 90% fail

    Coroutines are not meant to be manually driven; it's complex and error-prone.