Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use PublisherServiceAsyncClient #1027

Open
skhaz opened this issue Nov 26, 2023 · 11 comments
Open

How to use PublisherServiceAsyncClient #1027

skhaz opened this issue Nov 26, 2023 · 11 comments
Assignees
Labels
api: pubsub Issues related to the googleapis/python-pubsub API.

Comments

@skhaz
Copy link

skhaz commented Nov 26, 2023

I'm currently working on an asynchronous project and seeking guidance on implementing PublisherServiceAsyncClient for message publishing in PubSub. Given that we're in the year 2023 and asyncio is widely used, the traditional client doesn't seem feasible for our needs. Could you please provide an example of how PublisherServiceAsyncClient can be used in an asyncio context?

Thank you in advance.

@product-auto-label product-auto-label bot added the api: pubsub Issues related to the googleapis/python-pubsub API. label Nov 26, 2023
@adriangb
Copy link

adriangb commented Dec 7, 2023

This would be great.

@pradn
Copy link
Contributor

pradn commented Dec 7, 2023

There's an auto-generated async client here. A sample for using it is here.

However, I don't recommend using it. The auto-generated layers do not have the custom code required to support complex features like publishing with ordering keys (see sample here). There are other smaller differences as well. The Publish call here returns a future, so you could possible massage that into the async API you prefer.

@adriangb
Copy link

adriangb commented Dec 7, 2023

More documentation and some higher level wrappers are needed. I’m an advanced python user and kept running into issues with rate limiting, etc. I probably needed to implement exponential backoff and such but gave up and run the sync client in threads.

In general the documentation for this library is quite poor. It’s mostly API docs and a handful of examples with little explanation.

@skhaz
Copy link
Author

skhaz commented Dec 7, 2023

I agree. I just gave up on using these APIs.

@pradn
Copy link
Contributor

pradn commented Dec 13, 2023

@adriangb :

kept running into issues with rate limiting, etc.

Can you please give me some more detail about this?

I probably needed to implement exponential backoff

The lower-level APIs will automatically do retries for you, with the parameters/settings specified here.

In general the documentation for this library is quite poor. It’s mostly API docs and a handful of examples with little explanation.

Sorry about this - we're especially weak in async API docs. I'll bring this up with leads and see what we can do. We haven't gotten that much demand for async APIs generally over the past few years, but this will likely continue to increase as it becomes more idiomatic.

@pradn
Copy link
Contributor

pradn commented Dec 13, 2023

@skhaz

I just gave up on using these APIs.

Can you please give us some more detail about the specific issues you saw? We, of course, want to make the libraries easy to use.

@adriangb
Copy link

Can you please give me some more detail about this?

Well I didn't spend that much time troubleshooting but I can say I was getting a lot of 503 and other errors and moving the sync APIs fixed it. Was it because I didn't put sleeps between yielding pull requests? Not sure, there's not docs on it...

@pradn
Copy link
Contributor

pradn commented Dec 13, 2023

503 Unavailable errors should be temporary backend errors. They'll be retried under the hood, but at some point the error will bubble up to you. You should generally use exponential backoff, but the retry schedule is up to you. If you have sample code, I can take a look. :)

@steve-marmalade
Copy link

Linking my comment here in case it helps anyone else who wants to use GCP Pub/Sub in an async environment (such as a FastAPI web server): #389 (comment)

@skhaz
Copy link
Author

skhaz commented Mar 21, 2024

This is not async.

@steve-marmalade
Copy link

This is not async.

@skhaz , is this in response to my comment? If so, can you expand a little bit on what you mean?

Here's what I'm suggesting:

from google.cloud import pubsub_v1

publisher_client = pubsub_v1.PublisherClient()
topic_path = "..."

async def send_message():
  future = publisher_client.publish(topic_path, b'Hello, World')
  message_id = await asyncio.wrap_future(future)
  return message_id

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: pubsub Issues related to the googleapis/python-pubsub API.
Projects
None yet
Development

No branches or pull requests

5 participants