Debian Codegraph @

docs/advanced/timeouts.md

raw
1 HTTPX is careful to enforce timeouts everywhere by default.
2
3 The default behavior is to raise a `TimeoutException` after 5 seconds of
4 network inactivity.
5
6 ## Setting and disabling timeouts
7
8 You can set timeouts for an individual request:
9
10 ```python
11 # Using the top-level API:
12 httpx.get('http://example.com/api/v1/example', timeout=10.0)
13
14 # Using a client instance:
15 with httpx.Client() as client:
16 client.get("http://example.com/api/v1/example", timeout=10.0)
17 ```
18
19 Or disable timeouts for an individual request:
20
21 ```python
22 # Using the top-level API:
23 httpx.get('http://example.com/api/v1/example', timeout=None)
24
25 # Using a client instance:
26 with httpx.Client() as client:
27 client.get("http://example.com/api/v1/example", timeout=None)
28 ```
29
30 ## Setting a default timeout on a client
31
32 You can set a timeout on a client instance, which results in the given
33 `timeout` being used as the default for requests made with this client:
34
35 ```python
36 client = httpx.Client() # Use a default 5s timeout everywhere.
37 client = httpx.Client(timeout=10.0) # Use a default 10s timeout everywhere.
38 client = httpx.Client(timeout=None) # Disable all timeouts by default.
39 ```
40
41 ## Fine tuning the configuration
42
43 HTTPX also allows you to specify the timeout behavior in more fine grained detail.
44
45 There are four different types of timeouts that may occur. These are **connect**,
46 **read**, **write**, and **pool** timeouts.
47
48 * The **connect** timeout specifies the maximum amount of time to wait until
49 a socket connection to the requested host is established. If HTTPX is unable to connect
50 within this time frame, a `ConnectTimeout` exception is raised.
51 * The **read** timeout specifies the maximum duration to wait for a chunk of
52 data to be received (for example, a chunk of the response body). If HTTPX is
53 unable 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 of
55 data to be sent (for example, a chunk of the request body). If HTTPX is unable
56 to send data within this time frame, a `WriteTimeout` exception is raised.
57 * The **pool** timeout specifies the maximum duration to wait for acquiring
58 a connection from the connection pool. If HTTPX is unable to acquire a connection
59 within this time frame, a `PoolTimeout` exception is raised. A related
60 configuration here is the maximum number of allowable connections in the
61 connection pool, which is configured by the `limits` argument.
62
63 You can configure the timeout behavior for any of these values...
64
65 ```python
66 # A client with a 60s timeout for connecting, and a 10s timeout elsewhere.
67 timeout = httpx.Timeout(10.0, connect=60.0)
68 client = httpx.Client(timeout=timeout)
69
70 response = client.get('http://example.com/')
71 ```