View a markdown version of this page

Synchronous and asynchronous clients - AWS SDK for Python v4

Developer Preview: This documentation covers the AWS SDK for Python v4, which is in Developer Preview and intended for evaluation and testing only. Do not use it for production workloads. For production applications, use the AWS SDK for Python (Boto3). To understand the differences between the two SDKs, see Choosing the right AWS SDK for Python.

Synchronous and asynchronous clients

Every service package of the AWS SDK for Python v4 contains a synchronous client and an asynchronous client. A synchronous client call blocks until the response arrives. An asynchronous client call returns a coroutine that you await on an asyncio event loop. Both clients take the same keyword arguments and use the same model classes from the package's models module.

Importing and using each client

The synchronous classes are named after the service, such as S3Client and S3Config. The asynchronous classes have an Async prefix, such as AsyncS3Client and AsyncS3Config. Import the client and configuration classes from the top-level package, such as from aws_sdk_s3 import S3Client, S3Config. Import request and response types from the package's models module. The following table compares the two DynamoDB clients.

Task Synchronous client Asynchronous client
Import the client and configuration from aws_sdk_dynamodb import DynamoDBClient, DynamoDBConfig from aws_sdk_dynamodb import AsyncDynamoDBClient, AsyncDynamoDBConfig
Resolve the configuration config = DynamoDBConfig.resolve(region="us-east-1") config = await AsyncDynamoDBConfig.resolve(region="us-east-1")
Open the client in a block with DynamoDBClient(config=config) as client: async with AsyncDynamoDBClient(config=config) as client:
Call an operation client.list_tables(limit=25) await client.list_tables(limit=25)
Close the client explicitly client.close() await client.close()
Import models from aws_sdk_dynamodb.models import ... from aws_sdk_dynamodb.models import ...

Both clients hold network resources until you close them. Leaving the with or async with block closes the client. If you call an operation on a closed client, the client raises a RuntimeError. The following example lists DynamoDB tables with each client.

Synchronous client
from aws_sdk_dynamodb import DynamoDBClient, DynamoDBConfig def main() -> None: config = DynamoDBConfig.resolve(region="us-east-1") with DynamoDBClient(config=config) as client: response = client.list_tables(limit=25) print(response.table_names or []) if __name__ == "__main__": main()
Asynchronous client
import asyncio from aws_sdk_dynamodb import AsyncDynamoDBClient, AsyncDynamoDBConfig async def main() -> None: config = await AsyncDynamoDBConfig.resolve(region="us-east-1") async with AsyncDynamoDBClient(config=config) as client: response = await client.list_tables(limit=25) print(response.table_names or []) if __name__ == "__main__": asyncio.run(main())

Choosing a client type

Use the synchronous client for scripts, AWS Lambda function handlers, and code that is already synchronous. It doesn't need an event loop.

Use the asynchronous client if your application already runs on asyncio, if it runs many requests concurrently, or if it uses event streams. Event-stream operations exist only on the asynchronous client. For more information, see Using the SDK asynchronously, Running concurrent operations, and Working with event streams.

Using components that match the client mode

Some components that you pass to a client have a synchronous form and an asynchronous form. Use components that match the client mode. A synchronous client raises an error when it receives an asynchronous component. An asynchronous client also accepts synchronous credential resolvers and endpoint resolvers.

Component Synchronous client Asynchronous client
HTTP client smithy_http.urllib3.Urllib3HTTPClient smithy_http.aio.aiohttp.AIOHTTPClient or smithy_http.aio.crt.AWSCRTHTTPClient
Credential resolvers Classes in smithy_aws_core.identity, such as EnvironmentCredentialsResolver Classes in smithy_aws_core.aio.identity, such as AsyncEnvironmentCredentialsResolver
Endpoint resolvers Implementations of smithy_core.interfaces.EndpointResolver Implementations of smithy_core.aio.interfaces.AsyncEndpointResolver
Mock HTTP client for tests smithy_http.testing.MockHTTPClient smithy_http.aio.testing.AsyncMockHTTPClient

For more information, see HTTP configuration, Credential providers, Use a custom endpoint resolver, and Testing applications that use the SDK.