我正在尝试使用异步客户端库(该库也由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)
(我已经从上面链接的页面上复制/粘贴了代码(。
为了运行该代码,我将其稍微调整为:
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())
当我运行它时,它在以下行失败:async for response in page_result:
出现错误:
"同步for"需要一个具有aiter方法的对象,得到协同程序
这是我第一次尝试异步python开发,鉴于我(认为我(已经完全遵循了提供的代码,我不知道为什么会出现这个错误。
我是不是遗漏了一些显而易见的东西?有人能解释一下如何克服这个错误吗?
尝试使用"等待":
# Make the request
page_result = await client.list_featurestores(request=request)