Python Timeouts
An unresponsive service can be worse than a down one. It can tie up your entire system if not handled properly. All network requests should have a timeout.
Here’s how to add timeouts for popular Python packages. All have been tested. The default is no timeout, unless otherwise specified. Enjoy!
Packages
Standard library
PyPI
- aiohttp
- asyncpg
- boto3
- elasticsearch
- mongoengine
- mysqlclient
- opensearch-py
- psycopg
- psycopg2
- pymemcache
- pymongo
- redis
- requests
- SQLAlchemy
Standard Library
http
HTTPConnection(host, port, timeout=1)
Raises socket.timeout
smtplib
SMTP(host, timeout=1)
Raises
socket.timeout
on connect timeoutsmtplib.SMTPServerDisconnected
on read timeout
socket
sock.settimeout(1)
Raises socket.timeout
subprocess
subprocess.run(cmd, timeout=1)
Raises subprocess.TimeoutExpired
PyPI
aiohttp
timeout = aiohttp.ClientTimeout(total=1)
async with aiohttp.ClientSession(timeout=timeout) as session:
# ...
Raises asyncio.exceptions.TimeoutError
asyncpg
asyncpg.connect(timeout=1)
Default: 60s
Raises asyncio.exceptions.TimeoutError
boto3
boto3.client('s3', config=Config(connect_timeout=1, read_timeout=1))
Raises
botocore.exceptions.ConnectTimeoutError
on connect timeoutbotocore.exceptions.ReadTimeoutError
on read timeout
elasticsearch
Elasticsearch(timeout=1)
Raises elasticsearch.exceptions.ConnectionError
mongoengine
connect(connectTimeoutMS=1000, socketTimeoutMS=1000, serverSelectionTimeoutMS=1000)
Raises pymongo.errors.ServerSelectionTimeoutError
mysqlclient
MySQLdb.connect(connect_timeout=1)
Raises MySQLdb._exceptions.OperationalError
opensearch-py
OpenSearch(timeout=1)
Raises opensearchpy.exceptions.ConnectionError
psycopg
psycopg.connect(connect_timeout=1)
Raises psycopg.OperationalError
psycopg2
psycopg2.connect(connect_timeout=1)
Raises psycopg2.OperationalError
pymemcache
Client(host, connect_timeout=1, timeout=1)
Raises socket.timeout
pymongo
MongoClient(connectTimeoutMS=1000, socketTimeoutMS=1000, serverSelectionTimeoutMS=1000)
Default: 20s connect timeout, 30s server selection timeout
Raises pymongo.errors.ServerSelectionTimeoutError
redis
Redis(socket_connect_timeout=1, socket_timeout=1)
Raises redis.exceptions.TimeoutError
requests
requests.get(url, timeout=1)
Raises
requests.exceptions.ConnectTimeout
on connect timeoutrequests.exceptions.ReadTimeout
on read timeout
SQLAlchemy
create_engine(url, connect_args={'connect_timeout': 1})
Raises sqlalchemy.exc.OperationalError
Don’t see a library you use?
Let us know. Even better, create a pull request for it.
Running the Tests
git clone https://github.com/ankane/python-timeouts.git
cd python-timeouts
pip install -r requirements.txt
To run all tests, use:
pytest
To run individual tests, use:
pytest tests/test_redis.py