为什么会失败并显示“'async for'需要一个带有 __aiter__ 方法的对象,得到协程”

问题描述 投票:0回答:1

我正在尝试使用异步客户端库(该库也是由 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:
有错误:

'async for' 需要一个带有 aiter 方法的对象,得到协程

这是我第一次涉足异步 python 开发,鉴于我(认为我)已经严格遵循了提供的代码,我不知道为什么会收到此错误。

我在这里遗漏了一些明显的东西吗?有人可以解释如何克服这个错误吗?

python python-asyncio google-cloud-ai
1个回答
0
投票

尝试使用“等待”:

# Make the request
page_result = await client.list_featurestores(request=request)
© www.soinside.com 2019 - 2024. All rights reserved.