All Products
Search
Document Center

Alibaba Cloud SDK:Configure a timeout period

Last Updated:Jul 08, 2024

This topic describes how to configure a timeout period in Alibaba Cloud SDK for Python V2.0.

Methods

Note

The priority of timeout period settings are listed in descending order: settings configured by using RuntimeOptions, settings configured by using a Config, and default settings.

  • Use default settings. The default timeout period for connection requests is 5,000 milliseconds and the default timeout period for read requests is 10,000 milliseconds.

  • Use RuntimeOptions.

    import os
    
    from alibabacloud_ecs20140526.client import Client as EcsClient
    from alibabacloud_ecs20140526.models import DescribeRegionsRequest
    from alibabacloud_tea_openapi.models import Config
    from alibabacloud_tea_util.models import RuntimeOptions
    
    config = Config(
        access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
        access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
        endpoint='ecs-cn-hangzhou.aliyuncs.com'
    )
    ecs_client = EcsClient(config)
    # The timeout period settings take effect only for requests that use RuntimeOptions.
    runtimeOptions = RuntimeOptions(
        read_timeout=10000 # Configure the timeout period for read requests. Unit: millisecond.
        connect_timeout=5000 # Configure the timeout period for connection requests. Unit: millisecond.
    )
    request = DescribeRegionsRequest()
    response = ecs_client.describe_regions_with_options(request, runtimeOptions)
    print(response.body)
    
  • Use a Config.

    import os
    
    from alibabacloud_ecs20140526.client import Client as EcsClient
    from alibabacloud_ecs20140526.models import DescribeRegionsRequest
    from alibabacloud_tea_openapi.models import Config
    from alibabacloud_tea_util.models import RuntimeOptions
    
    # The timeout period settings take effect for SDK clients that are initialized by using the Config.
    config = Config(
        access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
        access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
        endpoint='ecs-cn-hangzhou.aliyuncs.com',
        read_timeout=10000 # Configure the timeout period for read requests. Unit: millisecond.
        connect_timeout=5000 # Configure the timeout period for connection requests. Unit: millisecond.
    )
    ecs_client = EcsClient(config)
    runtimeOptions = RuntimeOptions()
    request = DescribeRegionsRequest()
    response = ecs_client.describe_regions_with_options(request, runtimeOptions)
    print(response.body)