import asyncio
async def slow_operation(n):
await asyncio.sleep(n)
print('Slow operation {} complete'.format(n))
return n
loop = asyncio.get_event_loop()
done, _ = loop.run_until_complete(
asyncio.wait([
slow_operation(1),
slow_operation(2),
slow_operation(9),
slow_operation(2),
slow_operation(1),
slow_operation(2),
slow_operation(3),
]))
for fut in done:
print("return value is {}".format(fut.result()))
Slow operation 1 complete
Slow operation 1 complete
Slow operation 2 complete
Slow operation 2 complete
Slow operation 2 complete
Slow operation 3 complete
Slow operation 9 complete
return value is 2
return value is 1
return value is 2
return value is 1
return value is 9
return value is 3
return value is 2