victory
victory

Reputation: 171

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

I am using asyncio.gather, and trying to retry when there is exception. 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

Upvotes: 5

Views: 10858

Answers (1)

Ahmed AEK
Ahmed AEK

Reputation: 17616

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.

Upvotes: 6

Related Questions