All Products
Search
Document Center

Alibaba Cloud SDK:Configure a retry mechanism

Last Updated:Jul 09, 2024

This topic describes how to configure a retry mechanism in Alibaba Cloud for Python V2.0

Note

In Alibaba Cloud SDK V2.0, the API request processing logic includes the built-in retry logic for network exceptions. When a network exception occurs, the system automatically retries to reinitiate the request to ensure service stability and reliability. If an error is returned due to a business logic error, such as a parameter error or unavailable resources, the system performs no retries. In this case, you must adjust the request based on the corresponding error information instead of performing retries.

Methods

Note

The retry mechanism that you configure by using RuntimeOptions takes precedence over the default retry setting.

  • Use default retry settings. By default, no retries are performed when exceptions occur. If you enable retries but do not specify the maximum number of retries, up to three retries are performed by default.

  • 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)
    runtimeOptions = RuntimeOptions(
        autoretry=True,  # Specifies whether to enable retries. Retries are disabled by default.
        max_attempts=3   # The number of maximum retry attempts. Default value: 3.
    )
    request = DescribeRegionsRequest()
    response = ecs_client.describe_regions_with_options(request, runtimeOptions)
    print(response.body)