Giving Up on a Slow Kiosk With Promise.race()
Hollins Green’s kiosk takes 0.9 seconds to answer, and Owen’s board shouldn’t wait that long. His
Python script gives each kiosk half a second. Its report() wraps each check in
asyncio.wait_for() and catches TimeoutError, and Quarry Hill’s failure has its own except
clause:
async def report(branch):
try:
kiosk = await asyncio.wait_for(
check_kiosk(branch), timeout=LIMIT
)
print(f"{branch}: {kiosk['holds']} holds waiting")
except TimeoutError:
print(f"{branch}: no answer in {LIMIT} s")
except ConnectionError as err:
print(err)
main() runs every report at once with asyncio.gather() and times the run. For this lesson, the
stand-in also prints [kiosk] <branch> answered before it returns. Python’s board takes half a
second, and the script ends there:
Quarry Hill kiosk is not answering
[kiosk] Northgate answered
Northgate: 14 holds waiting
[kiosk] Eastfield answered
Eastfield: 9 holds waiting
Hollins Green: no answer in 0.5 s
Checked 4 kiosks in 0.5 s