Search code examples
pythonazureasynchronouspython-asyncioazure-table-storage

Retrieve data from multiple different Azure Tables asynchronously with Azure Tables client library


Is it possible with the Azure Tables client library for Python to retrieve data from multiple tables asynchronously? Let's say I have table A and B in different storage accounts, is it possible to retrieve data from both tabels simultaneously levering the asyncio module?

I cannot find any documentation specifying whether this is possible or how to implement this. I can think of building two async functions that can retrieve data from the tables and calling them via asyncio.gather(). Would this work or can the actual outbound call to the Azure endpoint not be done asynchronously?

I see that there exists a Azure Data Tables aio module which might be leveraged for this purpose?


Solution

  • I tried in my environment and got below results:

    You can use the below code to retrieve the data for two tables in different storage accounts using asyncio method.

    Code:

    import asyncio
    from azure.data.tables.aio import TableServiceClient
    
    async def retrieve_table_data1(table_name):
        async with TableServiceClient.from_connection_string("<connect_string1>") as table_service:
            async with table_service.get_table_client(table_name) as table_client:
                async for entity in table_client.list_entities():
                    print(entity)
    
    async def retrieve_table_data2(table_name):
        async with TableServiceClient.from_connection_string("<connect_string2>") as table_service:
            async with table_service.get_table_client(table_name) as table_client:
                async for entity in table_client.list_entities():
                    print(entity)
    
    async def main():
        await asyncio.gather(
            retrieve_table_data1("table1"),
            retrieve_table_data2("table2"),
        )
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    asyncio.run(main())
    

    Console:

    The above code executed successfully and retrieved data from two tables.

    enter image description here