
Checkpoint
Checkpoint: Night Shift Ledger
The night-shift clerk has two jobs.
First, pick the right tool for each job in tonight's plan. Each job
says how long it spends waiting (wait_ms) and computing
(cpu_ms) per item, and how many items there are (count).
Second, record fuel payments in the guild ledger. Many payments arrive at once as async tasks. Each one reads a balance, waits for a slow write, then saves the new balance. You know what that means: a race condition, unless a lock guards it.
Assignment
pick_tool(job)returns:"plain"ifcountis 1 or less (check this first);"processes"ifcpu_msis greater thanwait_ms;"asyncio"otherwise.
- Fix
recordso the whole read, wait, write runs insideasync with lock:. Keep theawait asyncio.sleep(0). - Leave
run_dayas it is. With the lock in place, it returns the right totals.
pick_tool(job)returns:"plain"ifcountis 1 or less (check this first);"processes"ifcpu_msis greater thanwait_ms;"asyncio"otherwise.
- Fix
recordso the whole read, wait, write runs insideasync with lock:. Keep theawait asyncio.sleep(0). - Leave
run_dayas it is. With the lock in place, it returns the right totals.
main.py Python sleeping
Press Run to try your code, then Check to test it.