Interview Prep/Python for DevOps/Concurrency: threads, processes & asyncio

How do you add timeouts to async operations?

IntermediateConceptConcurrency: threads, processes & asyncio
Answer
try:
    async with asyncio.timeout(10):            # 3.11+
        data = await fetch_all()
except TimeoutError:
    log.error("fetch_all exceeded 10s")

result = await asyncio.wait_for(fetch_one(), timeout=3)   # older API

A timeout cancels the inner task by raising CancelledError inside it, so cleanup code in finally blocks runs. Also set client-level timeouts (httpx/aiohttp) so individual requests cannot hang.

All Python for DevOps questions Roadmap: Python for automation

More on Concurrency: threads, processes & asyncio