Running tasks together with gather
await one coroutine after another and they run one after another. To run
them concurrently, hand them all to asyncio.gather:
results = await asyncio.gather(fetch(1), fetch(2), fetch(3))
gather starts every coroutine, waits until all are done, and returns their
results as a list in the order you passed them, whichever finished first.
The program runs three tasks that each wait 0.1 s. One after another, that's
0.3 s. With gather, the waits overlap, so it takes about 0.1 s. Same work,
same single thread.
If one task raises, gather raises that error. Pass return_exceptions=True to
get errors back in the result list instead. Python 3.11 also added
asyncio.TaskGroup, which cancels the other tasks when one fails.
Run it and compare the two timings. Then change the delays to see that the total is about the slowest task.