Command Palette

Search for a command to run...

Hectal
Phase 12: Concurrency and Async
Python · Exercise 3 of 6 PreviousNext
Advanced

Give up on slow calls with a timeout

Write async def answer_or_default(make_coro, seconds, default). It awaits make_coro(), but if that takes longer than seconds it stops waiting and returns default instead.

Use asyncio.timeout. Errors other than a timeout (such as ValueError) must still reach the caller. The program should print quick answer and then default.

What Check tests

  1. A fast call returns its own answer
  2. A slow call returns the default
  3. The slow call is cancelled and cleans up
  4. Any default value is returned as it is (even None)
  5. A ValueError still reaches the caller
  6. The program prints both answers

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.