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

标签 python azure asynchronous python-asyncio azure-table-storage

可以使用Azure Tables client library for Python吗?从多个表异步检索数据?假设我的表 A 和表 B 位于不同的存储帐户中,是否可以利用 asyncio 模块同时从两个表中检索数据?

我找不到任何文档来说明这是否可行或如何实现。我可以考虑构建两个异步函数,它们可以从表中检索数据并通过 asyncio.gather() 调用它们。这可行吗?或者对 Azure 端点的实际出站调用不能异步完成吗?

我发现存在一个 Azure Data Tables aio可以利用哪个模块来实现此目的?

最佳答案

我在我的环境中进行了尝试并得到了以下结果:

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

代码:

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())

控制台:

上面的代码执行成功,并从两个表中检索了数据。

enter image description here

关于python - 使用 Azure 表客户端库从多个不同的 Azure 表异步检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75373907/

相关文章:

python - Pyomo + asNMPC 框架

azure - 如何使用 AAD 身份验证在 powershell 中调用 Azure CosmosDB REST API

performance - Azure,中间表上的聚集索引和性能影响

C# webapi 异步上下文问题

c# - .Net Core Web Api 异步不重要吗?

python - 使用 scipy.optimize 动态选择参数以最小化 python 中的函数

python - 转换 Pandas 数据框

python - 从包含括号的文本中提取键值对(日志文件)

c# - 按类型或字符串类型强类型 Azure 移动服务表对象?

python - 什么时候不使用 asyncio 有意义?