3 ms·
Pythons asyncio.as_completed has a neat solution to this. By returning a placeholder-promise (awaitable), instead of the value they resolve to. It is only when
by Too 4y ago
Pythons asyncio.as_completed has a neat solution to this. By returning a placeholder-promise (awaitable), instead of the value they resolve to. It is only when you await this placeholder it eventually resolves the value of the first completed item in the loop. With this design you can choose yourself what do do if it fails.
for coro in asyncio.as_completed(awaitable_list):
earliest_result = await coro # Can be wrapped with try-block
I think same thing can be achieved with smart use of Promise.race. It's a bit different from the article, that wants to iterate in same order as the input. This can be added, by collecting the results and iterating them again, reinventing Promise.allSettled, i didn't fully understand why op couldn't use that.