I'm new to Python asyncio and I'm doing some experiments. I have the following code:
async def say_after(n, s):
await asyncio.sleep(n)
print(s)
async def main():
task1 = asyncio.create_task(say_after(2, 'a'))
task2 = asyncio.create_task(say_after(1, 'b'))
await task1
print('x', flush=True)
await task2
print('y', flush=True)
asyncio.run(main())
And the output:
b
a
x
y
I don't understand the order here. Can someone help explain? Especially why x comes after b and a?