Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 362
victory Avatar asked Aug 04 '26 20:08

victory


1 Answers

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.

like image 69
Ahmed AEK Avatar answered Aug 07 '26 08:08

Ahmed AEK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!