Configure a timeout period

Updated at:
Copy as MD

Timeout settings control how long the Alibaba Cloud SDK for Python V2.0 waits before abandoning a connection attempt or a read operation. Set timeouts long enough for your workload to complete, but short enough to surface failures quickly.

Timeout configuration

Note

Timeout settings follow this priority order, from highest to lowest: RuntimeOptions > Config > default values.

The SDK provides two timeout parameters, both specified in milliseconds (ms):

Parameter

Type

Default

Description

connect_timeout

int

5,000 ms

Maximum time to wait when establishing a connection.

read_timeout

int

10,000 ms

Maximum time to wait for a response after connecting.

The default values work well for most use cases. Override them only when you have a specific reason — for example, when calling an endpoint with consistently high latency, or when you need faster failure detection in a latency-sensitive application.

  • Use the defaults. If you do not set any timeout, the SDK uses a connection timeout of 5,000 ms and a read timeout of 10,000 ms.

  • Override per request using RuntimeOptions. This setting applies only to the individual request that uses the RuntimeOptions instance.

    from alibabacloud_tea_util.models import RuntimeOptions
    
    # Applies only to requests that use this RuntimeOptions instance.
    runtimeOptions = RuntimeOptions(
        read_timeout=10000,  # Read timeout in milliseconds (ms)
        connect_timeout=5000  # Connection timeout in milliseconds (ms)
    )
  • Override globally using the Config class. This setting applies to all clients initialized with this Config instance.

    from alibabacloud_tea_openapi.models import Config
    
    # Applies to all clients initialized with this Config instance.
    config = Config(
        read_timeout=10000,  # Read timeout in milliseconds (ms)
        connect_timeout=5000  # Connection timeout in milliseconds (ms)
    )