HTTPX is careful to enforce timeouts everywhere by default.
The default behavior is to raise a TimeoutException after 5 seconds of
network inactivity.
You can set timeouts for an individual request:
# Using the top-level API:
httpx.get('http://example.com/api/v1/example', timeout=10.0)
# Using a client instance:
with httpx.Client() as client:
client.get("http://example.com/api/v1/example", timeout=10.0)
Or disable timeouts for an individual request:
# Using the top-level API:
httpx.get('http://example.com/api/v1/example', timeout=None)
# Using a client instance:
with httpx.Client() as client:
client.get("http://example.com/api/v1/example", timeout=None)
You can set a timeout on a client instance, which results in the given
timeout being used as the default for requests made with this client:
client = httpx.Client() # Use a default 5s timeout everywhere.
client = httpx.Client(timeout=10.0) # Use a default 10s timeout everywhere.
client = httpx.Client(timeout=None) # Disable all timeouts by default.
HTTPX also allows you to specify the timeout behavior in more fine grained detail.
There are four different types of timeouts that may occur. These are connect, read, write, and pool timeouts.
ConnectTimeout exception is raised.ReadTimeout exception is raised.WriteTimeout exception is raised.PoolTimeout exception is raised. A related
configuration here is the maximum number of allowable connections in the
connection pool, which is configured by the limits argument.You can configure the timeout behavior for any of these values…
# A client with a 60s timeout for connecting, and a 10s timeout elsewhere.
timeout = httpx.Timeout(10.0, connect=60.0)
client = httpx.Client(timeout=timeout)
response = client.get('http://example.com/')