How does "await" make code flow in Python? This is the code to clear my question
import asyncio
async def io_related(name):
print(f'{name} started')
await asyncio.sleep(0.1)
print(f'{name} finished')
async def main():
await asyncio.gather(
io_related('1'),
io_related('2'),
io_related('3'),
io_related('4'),
)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
and I got this output:
"1 started
2 started
3 started
4 started
1 finished
3 finished
2 finished
4 finished"
Why "3" finished before "2" ?. How does the code flow? I tried putting "1", "2", "3" in the "await asyncio.gather". It works as I expected but after the item "4", the order of things "finished" is messy terribly.