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
- A fast call returns its own answer
- A slow call returns the default
- The slow call is cancelled and cleans up
- Any default value is returned as it is (even None)
- A ValueError still reaches the caller
- 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.