PythiloLesson 7 of 12 · progress kept in this browser

Timeouts and cancellation

Never wait forever on something outside your control. asyncio.timeout cancels the work inside it when time runs out and raises TimeoutError.

import asyncio

async def slow():
    await asyncio.sleep(10)

async def main():
    try:
        async with asyncio.timeout(0.01):
            await slow()
    except TimeoutError:
        print("gave up")

asyncio.run(main())

asyncio.wait_for(aw, seconds) does the same for a single awaitable.

Your task

Write async def with_timeout(aw, seconds, default) that returns the result of awaiting aw, or default if that takes longer than seconds. Write async def guarded(aw, log) that returns the result of awaiting aw; if it is cancelled, it appends "cancelled" to log and lets the cancellation continue.