import asyncio
import time
async def func():
print('task start')
await asyncio.sleep(10)
print('task end')
async def main():
task1 = asyncio.create_task(func())
task2 = asyncio.create_task(func())
task3 = asyncio.create_task(func())
task4 = asyncio.create_task(func())
s = time.monotonic()
print('main start', time.monotonic() - s)
await task1
print('main continue', time.monotonic() - s)
await task2
print('main continue', time.monotonic() - s)
await task3
print('main continue', time.monotonic() - s)
await task4
print('main end', time.monotonic() - s)
asyncio.run(main())
This code gives results as below:
main start 0.0
task start
task start
task start
task start
task end
task end
task end
task end
main continue 10.0
main continue 10.0
main continue 10.0
main end
But how is this possible ,python bypassed my prior print calls , run the awaitables first and then go back to make print calls , how shall I make sense of this ?