Timeout context manager for asyncio Python
Project description
aiotimeout
Timeout context manager for asyncio Python
Usage
from aiotimeout import timeout
# Will raise an asyncio.TimeoutError
with timeout(1):
await asyncio.sleep(1.5)
# Will not raise anything
with timeout(1):
await asyncio.sleep(0.5)
You can respond to a timeout from outside the context by catching asyncio.TimeoutError
try:
with timeout(1):
await asyncio.sleep(1.5)
print('This line is not reached')
except asyncio.TimeoutError:
print('Timed out')
or you can respond to a timeout from inside the context by catching asyncio.CancelledError
and re-raising.
try:
with timeout(1):
try:
await asyncio.sleep(1.5)
except asyncio.CancelledError
print('Doing some cleanup')
raise
except asyncio.TimeoutError:
print('Timed out')
Differences to alternatives
-
asyncio.wait_for
does not offer a context manager. In some cases a context manager is clearer. -
asyncio.wait_for
creates/uses an extra task. In some cases this is not necessary, and an extra task adds non-determinism in terms of sequence of operations. -
Clearer internal code [in the author's opinion]. Rather than a custom class, contextlib.contextmanager is used.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file aiotimeout-0.0.4.tar.gz
.
File metadata
- Download URL: aiotimeout-0.0.4.tar.gz
- Upload date:
- Size: 2.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.7.1 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f237259423f3dac001f5cf4f75c7637ba914b623186eefd49b6a3d3b47bba0b9 |
|
MD5 | 3e8c1bac5ac459eb93b2632f8c9e2998 |
|
BLAKE2b-256 | 909f822949c457bc1bd54694a241b72e11096399e3d75bc75413e8acd23b636d |
File details
Details for the file aiotimeout-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: aiotimeout-0.0.4-py3-none-any.whl
- Upload date:
- Size: 3.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.7.1 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 729eb5056bd424d5018429f7e338b195c2153348271a50ebaea6d95ec9c67185 |
|
MD5 | 798afe2cf2e058e8b710ebc0f52af538 |
|
BLAKE2b-256 | 8377102afcae8950b728e18141efc5b14ada3873f535eeedc4657ad93159ccbe |