July 12th, 2022

Run parallel CRUD operations with Python Async IO for Azure Cosmos DB

Khelan Modi
Product Manager

Async IO support for Azure Cosmos DB Python SDK, an important and much-anticipated capability, is now generally available (GA) for Python developers, data scientists, and any other professional that uses our Azure Cosmos DB Python SDK.

Benefits

Async IO support makes it easier to run multiple IO operations in parallel. 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 waiting 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.

Image Picture1

 

Image Picture2

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. The new asynchronous CosmosClient has the same look and feel as the existing synchronous client, but it entirely separate. However, the async client needs to be imported separately and its methods need to be used with the async/await keywords. The Async client needs to be initialized and closed after usage, which can be done manually or with the use of a context manager. The upsert example below shows how to do so manually.

from azure.cosmos.aio import CosmosClient
import os

URL = os.environ['ACCOUNT_URI']
KEY = os.environ['ACCOUNT_KEY']
DATABASE_NAME = 'testDatabase'
CONTAINER_NAME = 'products'    

async def create_products():
    client = CosmosClient(URL, credential=KEY)
    database = client.get_database_client(DATABASE_NAME)
    container = database.get_container_client(CONTAINER_NAME)
    for i in range(10):
        await container.upsert_item({
                'id': 'item{0}'.format(i),
                'productName': 'Widget',
                'productModel': 'Model {0}'.format(i)
            }
        )
    await client.close()

Instead of manually opening and closing the client, it is highly recommended to use the “async with” keywords. This creates a context manager that will initialize and later close the client once you’re out of the statement. The example below shows how to do so.

from azure.cosmos.aio import CosmosClient
import os

URL = os.environ['ACCOUNT_URI']
KEY = os.environ['ACCOUNT_KEY']
DATABASE_NAME = 'testDatabase'
CONTAINER_NAME = 'products'

async def create_products():
    async with CosmosClient(URL, credential=KEY) as client:
        database = client.get_database_client(DATABASE_NAME)
        container = database.get_container_client(CONTAINER_NAME)
        for i in range(10):
            await container.upsert_item({
                    'id': 'item{0}'.format(i),
                    'productName': 'Widget',
                    'productModel': 'Model {0}'.format(i)
                }
            )

Learn More

 

Find more

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! For more information about capabilities, limitations, syntax, and code samples for the Python SDK please check our GitHub page.

For more extensive documentation on the Azure Cosmos DB service, see the Azure Cosmos DB documentation.

Find more Azure Cosmos DB Python Async IO samples here.

Discover additional features and capabilities about Azure Cosmos DB and get started for free.

Author

Khelan Modi
Product Manager

Hey there! I'm Khelan Modi, Product Manager at Azure Cosmos DB. My mission is to build a better versatile-Core for our cosmic universe! Join me on this cosmic journey from Redmond, and together, we'll shoot for the stars! 🌠 Let's connect and explore the wonders of cloud tech!

2 comments

Discussion is closed. Login to edit/delete existing comments.

  • Wiliam Rosa

    Excellent article.
    Would it be possible to use this feature in other API’s, for example “cosmos db API Mongo”?
    Thanks.