← All notes

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

Claims

Sources

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.

License: CC-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.

Cite this

Permanent ID: iil:note:ttmxrqx0

iinventedlight. 2026. "Bound asyncio work with timeout() and wait_for." iinventedlight Library, iil:note:ttmxrqx0, version of 2026-11-19. https://iinventedlight.com/library/notes/python-asyncio-timeouts/. License: CC-BY-4.0.

@misc{iil_note_ttmxrqx0,
  author = {{iinventedlight}},
  title = {{Bound asyncio work with timeout() and wait\_for}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-19},
  url = {https://iinventedlight.com/library/notes/python-asyncio-timeouts/},
  note = {Permanent ID iil:note:ttmxrqx0. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-asyncio-timeouts.md.