Async IO for the Python SDK is now in preview!
Async IO support for Azure Cosmos DB Python SDK, an important and much-anticipated capability, is now available in preview for Python developers, data scientists, and any other professional that uses our Azure Cosmos DB Python SDK! Async IO is a concurrent programming method in Python that makes running CRUD (create, read, update, delete) operations in parallel easy to do.
Async IO support improves performance
The main advantage of Async IO support, like with most asynchronous clients, is to improve performance. Async IO makes it easy to run multiple IO operations (such as querying Azure Cosmos DB) at the same time, rather than wait for each operation to complete before running the next one. With Async IO support, the client can take advantage of the time that would normally be spent waiting for an IO response to run other tasks. This should be especially useful for users making several calls to Azure Cosmos DB within their applications.
Typical Python SDK Async IO scenarios
- A Developer writing an application that uploads or queries multiple documents in Azure Cosmos DB Core (SQL) API containers.
- A Data Scientist or Data Analyst reading big volumes of data from Azure Cosmos DB.
- A Data Engineer copying data from one Azure Cosmos DB container into a new container or to Azure storage.
How to get started?
To leverage Async IO, you need to use the new asynchronous CosmosClient, imported from azure.cosmos.aio instead of the regular synchronous client from azure.cosmos. Developers that have been using the synchronous client need to import the asynchronous client instead because sync clients can only do sync operations and async clients can only do async operations.
From the syntax perspective, there is one key difference, you need to use the async and await keywords to perform asynchronous operations. Here is how asynchronous point reads work. More examples can be found in the documentation.
# Import async client import asyncio from azure.cosmos.aio import CosmosClient
# Access credentials url = os.environ['ACCOUNT_URI'] key = os.environ['ACCOUNT_KEY']
# Client initialization client = CosmosClient(url, credential=key)
# To run, replace database and container with your db and container names database = client.get_database_client("database") container = database.get_container_client("container")
async def point_read(item_id): # Point Read data = await container.read_item(item=item_id, partition_key=item_id) # assumes that item_id is the partition key return data
async def main(): # To run, change item1 item2 and item3 to each document id print(await asyncio.gather(point_read("test1"), point_read("test2"), point_read("test3"))) await client.close()
Async IO in the Python SDK makes your programs run faster and your users happier with concurrent database operations. We’re looking forward to hearing about your experience using this preview! For more information about capabilities, limitations, syntax, and code samples for the Python SDK please check our GitHub page.