简体   繁体   English

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

[英]python asyncio.gather retrying returns "cannot reuse already awaited coroutine" error

I am using asyncio.gather, and trying to retry when there is exception.我正在使用 asyncio.gather,并在出现异常时尝试重试。 But it fails to retry with error like below.但它无法重试并出现如下错误。 How should I fix it to just retry?我应该如何修复它才能重试?

11 -- RETRY Server disconnected without sending a response.
11 -- RETRY cannot reuse already awaited coroutine
11 -- RETRY cannot reuse already awaited coroutine

My code:我的代码:

        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

a "task object" is only a "handle" to a task that was submitted to the event loop, awaiting on it doesn't create the task, it merely waits for the task that was already submitted to end, “任务对象”只是提交给事件循环的任务的“句柄”,等待它不会创建任务,它只是等待已经提交的任务结束,

you have to create a new task and await it, so you have to call whatever produced that tasks again, to produce another task for you to await.您必须创建一个新任务并等待它,因此您必须再次调用生成该tasks的任何内容,以生成另一个task供您等待。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM