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
Docs restructuring. (#3049)Tom Christiebefore adoption8cd952c
lines 1–71why, one step back1HTTPX is careful to enforce timeouts everywhere by default.23The default behavior is to raise a `TimeoutException` after 5 seconds of4network inactivity.56## Setting and disabling timeouts78You can set timeouts for an individual request:910```python11# Using the top-level API:12httpx.get('http://example.com/api/v1/example', timeout=10.0)1314# Using a client instance:15with httpx.Client() as client:16 client.get("http://example.com/api/v1/example", timeout=10.0)17```1819Or disable timeouts for an individual request:2021```python22# Using the top-level API:23httpx.get('http://example.com/api/v1/example', timeout=None)2425# Using a client instance:26with httpx.Client() as client:27 client.get("http://example.com/api/v1/example", timeout=None)28```2930## Setting a default timeout on a client3132You can set a timeout on a client instance, which results in the given33`timeout` being used as the default for requests made with this client:3435```python36client = httpx.Client() # Use a default 5s timeout everywhere.37client = httpx.Client(timeout=10.0) # Use a default 10s timeout everywhere.38client = httpx.Client(timeout=None) # Disable all timeouts by default.39```4041## Fine tuning the configuration4243HTTPX also allows you to specify the timeout behavior in more fine grained detail.4445There are four different types of timeouts that may occur. These are **connect**,46**read**, **write**, and **pool** timeouts.4748* The **connect** timeout specifies the maximum amount of time to wait until49a socket connection to the requested host is established. If HTTPX is unable to connect50within this time frame, a `ConnectTimeout` exception is raised.51* The **read** timeout specifies the maximum duration to wait for a chunk of52data to be received (for example, a chunk of the response body). If HTTPX is53unable to receive data within this time frame, a `ReadTimeout` exception is raised.54* The **write** timeout specifies the maximum duration to wait for a chunk of55data to be sent (for example, a chunk of the request body). If HTTPX is unable56to send data within this time frame, a `WriteTimeout` exception is raised.57* The **pool** timeout specifies the maximum duration to wait for acquiring58a connection from the connection pool. If HTTPX is unable to acquire a connection59within this time frame, a `PoolTimeout` exception is raised. A related60configuration here is the maximum number of allowable connections in the61connection pool, which is configured by the `limits` argument.6263You can configure the timeout behavior for any of these values...6465```python66# A client with a 60s timeout for connecting, and a 10s timeout elsewhere.67timeout = httpx.Timeout(10.0, connect=60.0)68client = httpx.Client(timeout=timeout)6970response = client.get('http://example.com/')71```