asyncio.gather
The asyncio.gather is the function that you will use the most. You pass to it multiple coroutines, it schedules them, waits for all to finish, and returns the list of results in the same order.
import asyncio
URLS = ['google.com', 'github.com', 't.me']
async def check_alive(url):
print(f'started {url}')
i = URLS.index(url)
await asyncio.sleep(3 - i)
print(f'finished {url}')
return i
async def main():
coros = [check_alive(url) for url in URLS]
statuses = await asyncio.gather(*coros)
for url, alive in zip(URLS, statuses):
print(url, alive)
asyncio.run(main())
Output:
started google.com
started github.com
started t.me
finished t.me
finished github.com
finished google.com
google.com 0
github.com 1
t.me 2
That's what happened:
asyncio.gatherschedules all tasks in order as they are passed.- We made the first task wait 3 seconds, the second wait 2 seconds, and the last one wait 1 second. And the tasks finished as soon as they could, without making everyone wait for the first task.
asyncio.gatherwaits for all tasks to finish.asyncio.gatherreturns a list of results in the order as the coroutines were passed in it. So, it's safe tozipresults with input values.