
Checkpoint
Checkpoint: Beacon Watch
At dusk, the night watch checks every beacon on the archipelago. Most answer quickly. The one on Fog Isle sometimes never answers at all.
The watch must check all beacons at the same time, and give each one only a short time to reply. A slow beacon must not hold up the rest.
The trick is a small guard helper. It runs one job inside its own timeout and records what happened:
async def guarded(...):
try:
async with asyncio.timeout(timeout):
result = await some_job()
# success: record the result
except TimeoutError:
# too slow: record the failure
...
Because each guard has its own timeout, one stalled beacon only cancels itself. You'll reuse this pattern in the course boss.
Assignment
guarded(name, delay, timeout, lit, missed): awaitbeacon(name, delay)with a limit oftimeoutseconds. If it finishes, store the result inlit[name]. If it times out, appendnametomissed. Don't let theTimeoutErrorescape.watch(beacons, timeout):beaconsmaps a name to a delay. Use anasyncio.TaskGroupto run oneguarded(...)task per beacon, all at the same time. Return(lit, sorted(missed)).
guarded(name, delay, timeout, lit, missed): awaitbeacon(name, delay)with a limit oftimeoutseconds. If it finishes, store the result inlit[name]. If it times out, appendnametomissed. Don't let theTimeoutErrorescape.watch(beacons, timeout):beaconsmaps a name to a delay. Use anasyncio.TaskGroupto run oneguarded(...)task per beacon, all at the same time. Return(lit, sorted(missed)).
main.py Python sleeping
Press Run to try your code, then Check to test it.