6

I am attempting to call an external API (one that is provided by Google) using an async client library (the library is also provided by Google).

The async method that I am attempting to call is async list_featurestores() (documentation). It provides the following sample code:

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)

(I've literally copy/pasted that code from the above linked page).

In order to run that code I've slightly adapted it to:

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())

When I run it it fails on this line: async for response in page_result: with error:

'async for' requires an object with aiter method, got coroutine

This is my first foray into async python development and, given I (think I) have followed the supplied code to the letter I don't know why I'm getting this error.

Am I missing something obvious here? Can someone explain how to get past this error?

jamiet
  • 10,501
  • 14
  • 80
  • 159
  • Hitting the same error with `documentai` service - these `list_xxx` methods do not seem to return true async generators. Did you find a workaround ? – yan-hic Nov 08 '22 at 15:17
  • As list is usually small, I used the blocking sync client to call the list method. These correctly return generators. – yan-hic Nov 08 '22 at 17:32
  • 1
    Why did you revert your post to a version with a broken link? – user2357112 Jun 09 '23 at 08:46
  • Which link is broken? I only see one link in this post (https://googleapis.dev/python/aiplatform/latest/aiplatform_v1/featurestore_service.html) and it is not broken. It does lead somewhere, even if it uses a redirect. I reverted your edit because it hyperlinked the word "here" which I dislike (this explains why: https://heyoka.medium.com/dont-use-click-here-f32f445d1021). – jamiet Jun 09 '23 at 12:17
  • 1
    Okay, display the URL if you prefer, but keep the nice link to the specific section... Also if you want a descriptive link *and* a less cluttered post, why not something like "([documentation] here)"? The article you linked to specifically recommends against using URLS themselves as links. – CrazyChucky Jun 09 '23 at 14:29
  • 1
    (Or just make the method itself a link, that is what you're linking to after all.) – CrazyChucky Jun 09 '23 at 14:40
  • good idea, done. – jamiet Jun 09 '23 at 15:52
  • Looks weird, their example seems broken. Maybe a package version problem? – HGLR Aug 24 '23 at 10:54

1 Answers1

0

Try using "await":

# Make the request
page_result = await client.list_featurestores(request=request)
AckThpfft
  • 51
  • 4