Книга: Learning Concurrency in Python
Назад: The gather(*coroes_or_futures, loop=None, return_exceptions=False) function
Дальше: Futures

The wait() function

The wait function simply blocks our program until all of the futures or coroutines passed into the first parameter of this function have successfully completed:

import asyncio

async def myCoroutine(i):
print("My Coroutine {}".format(i))

loop = asyncio.get_event_loop()
try:
tasks = []
for i in range(4):
tasks.append(myCoroutine(i))
loop.run_until_complete(asyncio.wait(tasks))
finally:
loop.close()

Upon execution of this program, you should see that we've successfully created our four coroutines, and they've all been successfully completed before our program terminated:

$ python3.6 16_asyncioTasks.py
My Coroutine 0
My Coroutine 1
My Coroutine 2
My Coroutine 3
Назад: The gather(*coroes_or_futures, loop=None, return_exceptions=False) function
Дальше: Futures