jam*_*iet 7 python python-asyncio google-cloud-ai
我正在尝试使用异步客户端库(该库也是由 Google 提供)调用外部 API(由 Google 提供的 API)。
我尝试调用的异步方法是async list_featurestores()(文档)。它提供了以下示例代码:
from google.cloud import aiplatform_v1
async def sample_list_featurestores():
# Create a client
client = aiplatform_v1.FeaturestoreServiceAsyncClient()
# Initialize request argument(s)
request = aiplatform_v1.ListFeaturestoresRequest(
parent="parent_value",
)
# Make the request
page_result = client.list_featurestores(request=request)
# Handle the response
async for response in page_result:
print(response)
Run Code Online (Sandbox Code Playgroud)
(我已经从上面的链接页面复制/粘贴了该代码)。
为了运行该代码,我对其进行了稍微调整:
import asyncio
from google.cloud import aiplatform_v1
async def sample_list_featurestores():
client = aiplatform_v1.FeaturestoreServiceAsyncClient()
request = aiplatform_v1.ListFeaturestoresRequest(parent="projects/MY_GCP_PROJECT/locations/europe-west2",)
page_result = client.list_featurestores(request=request)
async for response in page_result:
print(response)
if __name__ == "__main__":
asyncio.run(sample_list_featurestores())
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它在这一行失败:
async for response in page_result:错误:
'async for' 需要一个带有aiter方法的对象,得到协程
这是我第一次涉足异步 python 开发,鉴于我(认为我)已经严格遵循了提供的代码,我不知道为什么会收到此错误。
我在这里遗漏了一些明显的东西吗?有人可以解释如何克服这个错误吗?
小智 0
尝试使用“等待”:
# Make the request
page_result = await client.list_featurestores(request=request)
Run Code Online (Sandbox Code Playgroud)