A wait should not stop the rest of the program. asyncio keeps several waits open at once, so the time costs the slowest one instead of the sum of all of them.
What you will see
ok
About 2 min of reading
Quick check
Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.
Try It
Check what overlapping waits cost.
1
Sum of waits.
waits = [1, 2, 3]
print(sum(waits))
Before you run it, say out loud what it will print. Then open the answer.
What it prints
6
2
Slowest wait.
waits = [1, 2, 3]
print(max(waits))
Before you run it, say out loud what it will print. Then open the answer.
Before you run it, say out loud what it will print. Then open the answer.
What it prints
2
4
Task started.
import asyncio
async def main():
t = asyncio.create_task(asyncio.sleep(0))
await t
print("ok")
asyncio.run(main())
Before you run it, say out loud what it will print. Then open the answer.
What it prints
ok
Put it in order
Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.
Data Flow
The work is started, the loop keeps it open, another task runs while it waits, and the value is collected once it is ready.
1start the work
↓
2keep the task open
↓
3other tasks run
↓
4collect the value
Line by line
Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.
waits = [1, 2, 3]sets nothing on screen
print(sum(waits))6
Why this worksStart before you wait
The gap that matters is between starting the work and waiting for it. Start everything first, then wait, and the waits overlap.
A plain call is a wall
Any blocking call inside an async function freezes the loop. Push that work to a task or run it in a thread of its own.
Examples
Two waits in a row
import asyncio
asyncdefone():
await asyncio.sleep(1)
return1asyncdefmain():
a = awaitone()
b = awaitone()
print(a + b)
asyncio.run(main())
Each await waits for the one before it, so two sleeps pay twice.