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
- 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.
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
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.},
}
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.