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

  1. guarded(name, delay, timeout, lit, missed): await beacon(name, delay) with a limit of timeout seconds. If it finishes, store the result in lit[name]. If it times out, append name to missed. Don't let the TimeoutError escape.
  2. watch(beacons, timeout): beacons maps a name to a delay. Use an asyncio.TaskGroup to run one guarded(...) task per beacon, all at the same time. Return (lit, sorted(missed)).