全部产品
Search
文档中心

阿里云SDK:异步

更新时间:Jun 06, 2024

本文为您介绍V2.0 Python SDK中异步请求的调用方式。

V2.0 SDK异步

V2.0 SDK 支持异步请求 async def 定义异步方法,通过await client.{方法名}_async()调用SDK异步方法。

import asyncio
import os

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

'''云服务器示例'''


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())