In this python code that is mostly sample code for tasks and routins:
import asyncio
async def say_after(delay, what):
await asyncio.sleep(delay)
print(what)
async def main():
task1 = asyncio.create_task(
say_after(1, 'hello'))
task2 = asyncio.create_task(
say_after(2, 'world'))
await task2
asyncio.run(main())
I expect:
world
but I get:
hello
world
What am I doing wrong?
Await just stops your program and actually waits until the asynchronous task is finished. The chronology looks like this:
Now if we switch await task2 to await task1 it will look like this: