我正在嘗試異步讀取檔案,但我遇到了一個錯誤,說“ await only allowed in async function。” 據我了解,當await關鍵字在未標記為的函式中使用時會發生此錯誤async。但是,如下面的代碼所示,我將read_blob函式標記為async
我在 .ipynb Jupiter notebook 中運行它沒有任何問題(我在其中按單元格順序運行代碼),但是當我嘗試在 .py 檔案中的 VSCode 中運行它時,它會引發錯誤。
這是我的代碼:
from azure.storage.blob.aio import ContainerClient
import asyncio
from azure.core.exceptions import ResourceNotFoundError
from io import StringIO, BytesIO
class AsyncContainerClient(ContainerClient):
async def read_blob(self,
blob_name: str,
add_blob_name_col=False,
add_blob_date_col=False,
preprocessing_func=None,
zip_regex=r'. \.gz$',
csv_regex='. \.csv(\.gz)?$',
parquet_regex='. \.parquet$',
regex_string=None,
**kwargs):
assert isinstance(blob_name, str), f'{blob_name} is not a string'
try:
blob = (await self.download_blob(blob_name))
with BytesIO() as byte_stream:
await blob.readinto(byte_stream)
byte_stream.seek(0)
return pd.read_parquet(byte_stream, engine='pyarrow')
except ResourceNotFoundError:
return 0
blob_sas_url = "https://proan.blob"
acc = AsyncContainerClient.from_container_url(blob_sas_url)
test_dirs = ["models1/model.parquet", "models2/model.parquet", "models3/model.parquet"]
res = await asyncio.gather(*(acc.read_blob(f) for f in test_dirs))

uj5u.com熱心網友回復:
異步程式的入口點應該是asyncio.run,您應該將代碼包裝在異步方法中,然后呼叫它
from azure.storage.blob.aio import ContainerClient
import asyncio
from azure.core.exceptions import ResourceNotFoundError
from io import StringIO, BytesIO
class AsyncContainerClient(ContainerClient):
async def read_blob(self,
blob_name: str,
add_blob_name_col=False,
add_blob_date_col=False,
preprocessing_func=None,
zip_regex=r'. \.gz$',
csv_regex='. \.csv(\.gz)?$',
parquet_regex='. \.parquet$',
regex_string=None,
**kwargs):
assert isinstance(blob_name, str), f'{blob_name} is not a string'
try:
blob = (await self.download_blob(blob_name))
with BytesIO() as byte_stream:
await blob.readinto(byte_stream)
byte_stream.seek(0)
return pd.read_parquet(byte_stream, engine='pyarrow')
except ResourceNotFoundError:
return 0
async def main():
blob_sas_url = "https://proan.blob"
acc = AsyncContainerClient.from_container_url(blob_sas_url)
test_dirs = ["models1/model.parquet", "models2/model.parquet",
"models3/model.parquet"]
return await asyncio.gather(*(acc.read_blob(f) for f in test_dirs))
asyncio.run(main())
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/498344.html
