---
source: https://iinventedlight.com/library/notes/python-asyncio-timeouts.md
title: "Bound asyncio work with timeout() and wait_for"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); Public-Domain (Python PEPs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python docs, Coroutines and Tasks; Python PEPs, PEP 3156 – Asynchronous IO Support Rebooted: the “asyncio” Module. PSF-2.0; Public-Domain."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: asyncio timeouts and cancellation"
tags: [python, asyncio, timeout, cancellation, CancelledError, wait_for, shield]
client: Librarian
sources:
  - id: S1
    title: "Coroutines and Tasks"
    publisher: Python docs
    url: https://docs.python.org/3/library/asyncio-task.html
    accessed: 2026-11-19
    license: "PSF-2.0"
  - id: S2
    title: "PEP 3156 – Asynchronous IO Support Rebooted: the “asyncio” Module"
    publisher: Python PEPs
    url: https://peps.python.org/pep-3156/
    accessed: 2026-11-19
    license: "Public-Domain"
---
# Bound asyncio work with timeout() and wait_for
**Answer:** Use `asyncio.timeout(delay)` or `asyncio.wait_for(aw, timeout)` to wait for an awaitable to complete with a timeout. On timeout, asyncio cancels the work and raises `TimeoutError`. Catch `TimeoutError` outside the `timeout()` context manager. Timeouts, intervals and delays are measured in seconds, and may be ints or floats.

## timeout() versus wait_for()

`async with asyncio.timeout(delay):` wraps a block. `delay` may be `None`, meaning no time limit, and the manager can be rescheduled with `Timeout.reschedule()`. Prefer `asyncio.timeout()` or `asyncio.timeout_at()` rather than instantiating `Timeout` directly. If the awaited work takes too long, the context manager will cancel the current task, handle the resulting `asyncio.CancelledError`, and transform it into a `TimeoutError`.

`asyncio.wait_for(aw, timeout)` waits for an awaitable to complete with a timeout. If a timeout occurs, it cancels `aw` and raises `TimeoutError`. It waits until cancellation of the future actually finishes, and the total wait time may exceed the timeout. If the wait is cancelled, the future `aw` is also cancelled.

## Cancellation

A task can be cancelled safely. Cancellation raises `asyncio.CancelledError` in that task at the next opportunity. `CancelledError` directly subclasses `BaseException`. If it is explicitly caught, it should generally be propagated when clean-up is complete. Swallowing it can make `TaskGroup` and `timeout()` misbehave. To prevent `aw` from being cancelled, wrap it in `shield()`. A cancelled Future is considered done. For Tasks, cancellation may be delayed.

## See also

- [python-asyncio-basics](https://iinventedlight.com/library/notes/python-asyncio-basics/)
- [python-asyncio-awaitables](https://iinventedlight.com/library/notes/python-asyncio-awaitables/)
- [python-contextlib](https://iinventedlight.com/library/notes/python-contextlib/)

## Claims

- id: C1
  claim: asyncio.timeout cancels the current task when the delay is exceeded.
  source: S1
  evidence: "the context manager will cancel the current task and handle"

- id: C3
  claim: TimeoutError must be caught outside the timeout block.
  source: S1
  evidence: "the TimeoutError can only be caught outside of the context manager"

- id: C4
  claim: wait_for also cancels the awaitable and raises TimeoutError.
  source: S2
  evidence: "it cancels the task and raises TimeoutError"

## Sources

- [S1] Coroutines and Tasks. Python docs. https://docs.python.org/3/library/asyncio-task.html . License: PSF-2.0. Accessed 2026-11-19.

- [S2] PEP 3156 – Asynchronous IO Support Rebooted: the “asyncio” Module. Python PEPs. https://peps.python.org/pep-3156/ . License: Public-Domain. Accessed 2026-11-19.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, Coroutines and Tasks; Python PEPs, PEP 3156 – Asynchronous IO Support Rebooted: the “asyncio” Module. PSF-2.0; Public-Domain. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
