You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Azure Blob Storage SDK:异步调用query_blob()报错求助

解决Azure Blob Storage异步query_blob报错问题

错误原因

异步版本的query_blob返回AsyncBlobQueryReader异步可迭代对象,若直接对其进行解包操作(如a, b = await blob_client.query_blob(...)),或用同步方式遍历结果,就会触发cannot unpack non-iterable coroutine object错误。另外,异步方法调用后,读取结果也必须使用异步语法。

修正后的代码示例

from azure.storage.blob.aio import BlobServiceClient as BlobServiceClientAsync
from azure.storage.blob import BlobQueryParquetFormat, BlobQueryCsvFormat

async def query_blob_async(self): 
    blob_service_client = BlobServiceClientAsync.from_connection_string(
        self.connection_string_srct.get_secret_value()
    )
    async with blob_service_client:
        container_client = blob_service_client.get_container_client(self.container)
        blob_client = container_client.get_blob_client(blob)
        
        # 正确初始化格式对象
        parquet_format = BlobQueryParquetFormat()
        csv_format = BlobQueryCsvFormat(delimiter=",", quote_char='"')
        
        # 调用异步query_blob获取读取器
        data_reader = await blob_client.query_blob(
            f"SELECT * FROM BlobStorage where measurement_source_id in {in_string}",
            blob_format=parquet_format,
            output_format=csv_format,
        )
        
        # 异步遍历读取结果
        async for chunk in data_reader:
            # 处理单块数据,示例为UTF-8解码
            processed_chunk = chunk.decode('utf-8')
            # 这里添加你的业务逻辑,比如写入文件或解析数据
            print(processed_chunk)

关键注意事项

  • 必须确保parquet_format、csv_format是正确实例化的BlobQueryParquetFormat/BlobQueryCsvFormat对象,不能是未初始化的引用。
  • 异步读取器只能通过async for遍历,不能使用普通for循环。
  • 若之前代码存在对data_reader的解包操作,需完全移除,改用异步遍历处理结果。

内容的提问来源于stack exchange,提问作者Lewis O'Leary

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 22:02:00