Skip to content
Myr

Add timeout fine-tunning advanced docs section (#476)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Add timeout fine-tunning advanced docs section * Rephrase part of timeout documentation

authored by Mateusz Woś · committed by Florimond Manca · parent 84731c8 · Browse files · 2 files+75 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+75 added−0 removed

  • other:+75 added−0 removed

2 files+75 added−0 removed

modified:docs/advanced.md+73 added
165 unmodified lines
166167# This request will be tunneled instead of forwarded.168client.get("http://example.com")169```170171172## Timeout fine-tuning173HTTPX offers various request timeout management options. Three types of timeouts are available: **connect** timeouts, 174**write** timeouts and **read** timeouts.175176* The **connect timeout** specifies the maximum amount of time to wait until a connection to the requested host is established. 177If HTTPX is unable to connect within this time frame, a `ConnectTimeout` exception is raised.178* The **write timeout** specifies the maximum duration to wait for a chunk of data to be sent (for example, a chunk of the request body). 179If HTTPX is unable to send data within this time frame, a `WriteTimeout` exception is raised.180* The **read timeout** specifies the maximum duration to wait for a chunk of data to be received (for example, a chunk of the response body). 181If HTTPX is unable to receive data within this time frame, a `ReadTimeout` exception is raised.182183### Setting timeouts184You can set timeouts on two levels:185186- For a given request:187188```python189# Using top-level API190httpx.get('http://example.com/api/v1/example', timeout=5)191192# Or, with a client:193client = httpx.Client()194client.get("http://example.com/api/v1/example", timeout=5)195```196197- On a client instance, which results in the given `timeout` being used as a default for requests made with this client:198199```python200client = httpx.Client(timeout=5)201client.get('http://example.com/api/v1/example')202```203204Besides, you can pass timeouts in two forms:205206- A number, which sets the read, write and connect timeouts to the same value, as in the examples above. 207- A `TimeoutConfig` instance, which allows to define the read, write and connect timeouts independently:208209```python210timeout = httpx.TimeoutConfig(211 connect_timeout=5, 212 read_timeout=10, 213 write_timeout=15214)215216resp = httpx.get('http://example.com/api/v1/example', timeout=timeout)217```218219### Default timeouts220By default all types of timeouts are set to 5 second.221 222### Disabling timeouts223To disable timeouts, you can pass `None` as a timeout parameter. 224Note that currently this is not supported by the top-level API.225226```python227url = "http://example.com/api/v1/delay/10"228229httpx.get(url, timeout=None) # Times out after 5s230231232client = httpx.Client(timeout=None)233client.get(url) # Does not timeout, returns after 10s234235236timeout = httpx.TimeoutConfig(237 connect_timeout=5, 238 read_timeout=None, 239 write_timeout=5240)241httpx.get(url, timeout=timeout) # Does not timeout, returns after 10s242```
modified:docs/quickstart.md+2 added
378 unmodified lines
379```python380>>> httpx.get('https://github.com/', timeout=0.001)381```382383For advanced timeout management, see [Timeout fine-tuning](https://www.encode.io/httpx/advanced/#timeout-fine-tuning).384385## Authentication386387HTTPX supports Basic and Digest HTTP authentication.388
18 unmodified lines