python asyncio.gather 重试返回“无法重用已等待的协程”错误

vic*_*ory 4 python coroutine python-asyncio

我正在使用 asyncio.gather,并在出现异常时尝试重试。但它无法重试,并出现如下错误。我应该如何修复它以重试?

11 -- RETRY Server disconnected without sending a response.
11 -- RETRY cannot reuse already awaited coroutine
11 -- RETRY cannot reuse already awaited coroutine
Run Code Online (Sandbox Code Playgroud)

我的代码:

        for i in range((len(tasks) // TASK_CHUNK_SIZE) + 1):
            while True:
                try:
                    res = await asyncio.gather(*tasks[TASK_CHUNK_SIZE * i:TASK_CHUNK_SIZE * (i + 1)])
                    for rows in res:
                        f.writelines([f'{row}\n' for row in rows])
                    await asyncio.sleep(1)
                    break
                except Exception as e:
                    print(f'{(i + 1) * TASK_CHUNK_SIZE} -- RETRY {e}')

                    await asyncio.sleep(10)
                    continue
Run Code Online (Sandbox Code Playgroud)

Ahm*_*AEK 6

“任务对象”只是提交到事件循环的任务的“句柄”,等待它不会创建任务,它只是等待已经提交的任务结束,

您必须创建一个新任务并等待它,因此您必须tasks再次调用生成的任何任务,以生成另一个任务task供您等待。