python 使用Azure表客户端库从多个不同的Azure表异步检索数据

e0bqpujr  于 2023-02-11  发布在  Python
关注(0)|答案(1)|浏览(103)

Azure Tables client library for Python是否可以异步地从多个表中检索数据?假设表A和表B位于不同的存储帐户中,是否可以勒韦林asyncio模块同时从两个表中检索数据?
我找不到任何文档说明这是否可行或如何实现。我可以考虑构建两个异步函数,可以从表中检索数据,并通过asyncio.gather()调用它们。这是否可行,或者对Azure端点的实际出站调用是否不能异步完成?
我发现存在一个Azure Data Tables aio模块,可以用于此目的。

nue99wik

nue99wik1#

    • 我在我的环境中尝试,得到以下结果:**

您可以使用下面的代码,通过**asyncio method**检索不同存储帐户中的两个表的数据。

    • 代码:**
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())
    • 控制台:**

上述代码成功执行并从两个表中检索数据。

相关问题