Provides a timeout context manager for asyncio programs, allowing for easy management of timeouts in asynchronous code. Current version: 5.0.1. Maintained with regular updates.
Install & Compatibility
Where this runs
tested against v5.0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.925 runs
installs and imports cleanly · install 0.0s · import 0.212s · 17.8MB
glibcpy 3.10–3.925 runs
installs and imports cleanly · install 1.6s · import 0.182s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Timeout
✓ from async_timeout import timeout
Ensure correct import path to avoid ImportError
Demonstrates usage of the timeout context manager to set a 10-second timeout for an asynchronous operation.
import asyncio
from async_timeout import timeout
async def main():
async with timeout(10):
await asyncio.sleep(5)
print('Completed within timeout')
asyncio.run(main())
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'async_timeout'
The 'async_timeout' module is not installed in the Python environment.
fixInstall the module using pip: 'pip install async-timeout'.
ImportError: cannot import name 'timeout' from 'async_timeout'
The 'timeout' function is not available in the 'async_timeout' module, possibly due to an outdated version.
fixEnsure you have the latest version installed: 'pip install --upgrade async-timeout'.
AttributeError: module 'async_timeout' has no attribute 'timeout'
The 'timeout' attribute is missing from the 'async_timeout' module, likely due to an incorrect installation or version.
fixVerify the installation and version: 'pip show async-timeout' and upgrade if necessary.
TypeError: timeout() got an unexpected keyword argument 'timeout'
This issue arises when calling the `timeout` context manager with a keyword argument named `timeout`, while the function expects the duration as a positional argument or a different keyword like `delay`.
fixPass the timeout duration as the first positional argument, for example: `async with timeout(5.0):` instead of `async with timeout(timeout=5.0):`.
asyncio.TimeoutError
The asynchronous operation wrapped by the `async_timeout.timeout` context manager did not complete within the specified time limit, causing a `asyncio.TimeoutError` to be raised.
fixHandle the `asyncio.TimeoutError` exception using a `try...except` block, increase the `timeout` duration if the operation legitimately takes longer, or optimize the underlying asynchronous code to complete faster.
Audit
Dependencies
asynciorequiredRequired for asynchronous programming in Python