5

Async IO for the Python SDK is now in preview!

 2 years ago
source link: https://devblogs.microsoft.com/cosmosdb/async-python-sdk-preview/?WT_mc_id=DOP-MVP-4025064
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Async IO for the Python SDK is now in preview!

December 16th, 2021

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()  
asyncio.run(main())

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

Gahl Levy

Program Manager

Follow


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK