Timeouts - HTTPX (original) (raw)

HTTPX is careful to enforce timeouts everywhere by default.

The default behavior is to raise a TimeoutException after 5 seconds of network inactivity.

Setting and disabling timeouts

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) `

Setting a default timeout on a client

You can set a timeout on a client instance, which results in the giventimeout 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.

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.

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/') `