Retry task after task exception with asyncio.wait Retry task after task exception with asyncio.wait python-3.x python-3.x

Retry task after task exception with asyncio.wait


EDIT: the task itself could be the key in the map, which is cleaner.

async def run():    tasks = {asyncio.ensure_future(c()): c for c in (c1, c2, c3)}    pending = set(tasks.keys())    num_times_called = 0    while pending:        num_times_called += 1        print("{} times called with {} pending tasks: {}".format(num_times_called, len(pending), pending))        finished, pending = await asyncio.wait(pending, return_when=asyncio.FIRST_EXCEPTION)        for task in finished:            if task.exception():                print("{} got an exception {}, retrying".format(task, task.exception()))                coro = tasks[task]                new_task = asyncio.ensure_future(coro())                tasks[new_task] = coro                pending.add(new_task)        print("finished {}".format(finished))    print("finished all {}".format(time.time() - t))