Home

Awesome

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!

Also available for Ruby, Node, Go, and Rust

Build Status

Packages

Standard library

PyPI

Standard Library

ftplib

FTP(host, timeout=1)

Raises socket.timeout

http

HTTPConnection(host, port, timeout=1)

Raises socket.timeout

imaplib

IMAP4(host, timeout=1)

Raises socket.timeout

Note: Requires Python 3.9+

nntplib

NNTP(host, timeout=1)

Raises socket.timeout

poplib

POP3(host, timeout=1)

Raises socket.timeout

smtplib

SMTP(host, timeout=1)

Raises

socket

sock.settimeout(1)

Raises socket.timeout

subprocess

subprocess.run(cmd, timeout=1)

Raises subprocess.TimeoutExpired

telnetlib

Telnet(host, timeout=1)

Raises socket.timeout

urllib

urlopen(url, timeout=1)

Raises

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

cassandra-driver

Cluster([host], connect_timeout=1)

Raises cassandra.cluster.NoHostAvailable on connect timeout

elasticsearch

Elasticsearch(request_timeout=1)

Raises elastic_transport.ConnectionTimeout

httpx

httpx.get(url, timeout=1)
# or
httpx.Client(timeout=1)

Raises

influxdb

InfluxDBClient(timeout=1)

Raises

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

scs

sol = scs.solve(data, cone, time_limit_secs=1)

Check for a sol['info']['status'] of solved (inaccurate - reached time_limit_secs) for a timeout

SQLAlchemy

create_engine(url, connect_args={'connect_timeout': 1})

Raises sqlalchemy.exc.OperationalError

trino

trino.dbapi.connect(request_timeout=1)
# or
trino.dbapi.connect(request_timeout=(1, 1)) # (connect, read)

Raises trino.exceptions.TrinoConnectionError

typesense

Client({'connection_timeout_seconds': 1})

Raises

urllib3

http = urllib3.PoolManager(timeout=urllib3.Timeout(connect=1, read=1))
# or
http.request('GET', url, timeout=urllib3.Timeout(connect=1, read=1))

Raises urllib3.exceptions.MaxRetryError

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