All Products
Search
Document Center

Alibaba Cloud SDK:Send asynchronous requests

Last Updated:Jul 01, 2024

This topic describes how to send asynchronous requests in SDK for Python V2.0.

Method

SDK for Python V2.0 supports asynchronous requests. You can use the keyword async def to define an asynchronous method. You can call an expression in the await client.{Method name}_async() format to call the asynchronous method.

import asyncio
import os

from alibabacloud_ecs20140526.models import DescribeImagesRequest
from alibabacloud_ecs20140526.client import Client
from alibabacloud_tea_openapi.models import Config

In this example, a request is sent to call the Elastic Compute Service (ECS) API.


async def main():
    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'
    )
    client = Client(config)
    request = DescribeImagesRequest(
        region_id='cn-hangzhou'
    )

    response = await client.describe_images_async(request)
    print(response)
    return response


loop = asyncio.get_event_loop()
loop.run_until_complete(main())