gpt4 book ai didi

python - 当使用 asyncio.run 运行时,aio download_blob 可以工作一次,但不能工作两次

转载 作者:行者123 更新时间:2023-12-03 06:06:22 26 4
gpt4 key购买 nike

当使用 asyncio.run 运行时,azure blob 的代码 aio download_blob 工作一次但不是两次,这看起来像是与 iohttp 相关的错误,但不知道如何解决它。 (Windows)

我拥有的代码几乎是他们原始示例的副本: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/storage/azure-storage-blob/samples/blob_samples_hello_world_async.py

from azure.storage.blob.aio import ContainerClient
from azure.identity import DefaultAzureCredential
credentials = DefaultAzureCredential()

async def test(conn_client):
async with conn_client as client_conn:
stream = await client_conn.download_blob(my_path)
data = await stream.readall()
return data

if __name__ == "__main__":
my_container_name = "Container name"
my_client = ContainerClient.from_container_url(container_url=my_container_name, credential=credentials)
my_path = 'test_path'

data = asyncio.run(test(my_client)) # works and returns the file from blob storage
data2 = asyncio.run(test(my_client)) # doesn't work

错误消息:

DEBUG - asyncio: Using proactor: IocpProactor
...
await self.open()
File "C...\Cache\virtualenvs\transformer-wi-nHELc-py3.11\Lib\site-packages\azure\core\pipeline\transport\_aiohttp.py", line 127, in open
await self.session.__aenter__()
^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute '__aenter__'. Did you mean: '__delattr__'?

Process finished with exit code 1

有什么想法或解决方法吗?

最佳答案

aio download_blob works once but not twice when run with asyncio.run Any idea or workaround?

该错误表明 Azure SDK 代码正在尝试进入 None 的 session 。当 aiohttp session 在第二次下载操作之前关闭时,可能会发生这种情况。

您可以传递列表中的目标文件路径并使用该列表调用该函数。

这是我的代码,用于将 Blob 从 Blob 存储下载两次到本地环境。

代码:

from azure.storage.blob.aio import ContainerClient
from azure.identity import DefaultAzureCredential
import asyncio

async def download_blob(conn_client,path,l):
for i in l:
with open(i, "wb") as my_blob:
stream = await conn_client.download_blob(path)
data = await stream.readall()
my_blob.write(data)
my_blob.close()

async def main():
my_container_url = "https://xxxxx.blob.core.windows.net/test"
credentials = DefaultAzureCredential()
my_client = ContainerClient.from_container_url(container_url=my_container_url, credential=credentials)
my_path = 'your blob name'
l = [r"C:\Users\xx\xxxx\samp.csv",r"C:\Users\xxxxxx\samp1.csv"]
await download_blob(my_client, my_path,l)
await my_client.close()

if __name__ == "__main__":
asyncio.run(main())

执行上述代码并将 CSV 文件下载两次到我的本地文件夹。

文件: enter image description here

关于python - 当使用 asyncio.run 运行时,aio download_blob 可以工作一次,但不能工作两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77273922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com