timeouts.md
This file
Path
docs/advanced/timeouts.md
71 lines · 2.7 KiB · Markdown
Last changed by
Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071HTTPX is careful to enforce timeouts everywhere by default.
The default behavior is to raise a `TimeoutException` after 5 seconds ofnetwork inactivity.
## Setting and disabling timeouts
You can set timeouts for an individual request:
```python# 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:
```python# 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)```
## Setting a default timeout on a client
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:
```pythonclient = 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.```
## Fine tuning the configuration
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.
* The **connect** timeout specifies the maximum amount of time to wait untila socket connection to the requested host is established. If HTTPX is unable to connectwithin this time frame, a `ConnectTimeout` exception is raised.* The **read** timeout specifies the maximum duration to wait for a chunk ofdata to be received (for example, a chunk of the response body). If HTTPX isunable to receive data within this time frame, a `ReadTimeout` exception is raised.* The **write** timeout specifies the maximum duration to wait for a chunk ofdata to be sent (for example, a chunk of the request body). If HTTPX is unableto send data within this time frame, a `WriteTimeout` exception is raised.* The **pool** timeout specifies the maximum duration to wait for acquiringa connection from the connection pool. If HTTPX is unable to acquire a connectionwithin this time frame, a `PoolTimeout` exception is raised. A relatedconfiguration here is the maximum number of allowable connections in theconnection pool, which is configured by the `limits` argument.
You can configure the timeout behavior for any of these values...
```python# 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/')```