可以使用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())
控制台:
上面的代码执行成功,并从两个表中检索了数据。
关于python - 使用 Azure 表客户端库从多个不同的 Azure 表异步检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75373907/