python runtime_error ai_generated true

运行时错误:无法重用已被等待的协程

RuntimeError: cannot reuse already awaited coroutine

ID: python/asyncio-run-coroutine-twice

其他格式: JSON · Markdown 中文 · English
80%修复率
90%置信度
0证据数
2024-12-10首次发现

版本兼容性

版本状态引入弃用备注
3.7 active
3.8 active

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 100% 失败

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

  2. 90% 失败

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