Skip to content

超时

¥Timeouts

HTTPX 默认会在所有地方强制执行超时。

¥HTTPX is careful to enforce timeouts everywhere by default.

默认行为是引发TimeoutException网络不活动 5 秒后。

¥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

您可以在客户端实例上设置超时,这将导致给定timeout被用作此客户端发出的请求的默认设置:

¥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:

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 还允许您更详细地指定超时行为。

¥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.

  • 连接timeout 指定与请求主机建立套接字连接的最大等待时间。如果 HTTPX 无法在此时间段内连接,则ConnectTimeout引发异常。

    ¥The connect timeout specifies the maximum amount of time to wait until a socket connection to the requested host is established. If HTTPX is unable to connect within this time frame, a ConnectTimeout exception is raised.

  • 超时指定等待接收数据块(例如,响应主体的块)的最大持续时间。如果 HTTPX 无法在此时间段内接收数据,则会ReadTimeout引发异常。

    ¥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). If HTTPX is unable to receive data within this time frame, a ReadTimeout exception is raised.

  • 超时指定等待发送数据块(例如,请求主体的块)的最大持续时间。如果 HTTPX 无法在此时间段内发送数据,则会WriteTimeout引发异常。

    ¥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). If HTTPX is unable to send data within this time frame, a WriteTimeout exception is raised.

  • 水池timeout 指定从连接池获取连接的最大等待时间。如果 HTTPX 无法在此时间段内获取连接,则会PoolTimeout引发异常。这里相关的配置是连接池中允许的最大连接数,由limits争论。

    ¥The pool timeout specifies the maximum duration to wait for acquiring a connection from the connection pool. If HTTPX is unable to acquire a connection within this time frame, a PoolTimeout exception is raised. A related configuration here is the maximum number of allowable connections in the connection pool, which is configured by the limits argument.

您可以为这些值中的任何一个配置超时行为......

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