Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run synchronous pull in Google Cloud Pub/Sub with the Python client API

I can't find the returnImmediately flag in the Python client API. Is there any specific reason for that? Is there another way to pull queued message synchronously from a subscription in Python?

like image 674
Rupert Schiessl Avatar asked Oct 23 '17 19:10

Rupert Schiessl


People also ask

Is Pub sub synchronous?

Publish/subscribe messaging, or pub/sub messaging, is a form of asynchronous service-to-service communication used in serverless and microservices architectures. In a pub/sub model, any message published to a topic is immediately received by all of the subscribers to the topic.

How do I use Pubsub in Python?

To publish and receive messages from Pub/Sub in Python, you need to create a service account with the “Pub/Sub Publisher” and “Pub/Sub Subscriber” roles: After the service account is created, remember to create a JSON key for it, which will be used for authentication in our Python code later.

Is Google Pub/Sub push or pull?

Push subscription The Pub/Sub server sends each message as an HTTPS request to the subscriber client at a pre-configured endpoint. This request is shown as a PushRequest in the image.


1 Answers

Google doesn't provide something like this. But you can easily workaround it by implementing your own Queue

from Queue import Queue

from google.cloud import pubsub

subscriber = pubsub.SubscriberClient()
topic = "projects/newproject-xxxxx/topics/tarunlalwani"
subscription_name = 'projects/newproject-xxxxx/subscriptions/sub1'

class SynchronousSubscription(object):

    def callback(self, message):
        print(message.data)
        message.ack()
        self.pending_messages.put(message)

    def __init__(self, subscription):
        self.subscription_future = subscriber.subscribe(subscription_name, self.callback)
        self.pending_messages = Queue()

    def consume_sync(self):
        return self.pending_messages.get()

sub = SynchronousSubscription(subscription_name)
data = sub.consume_sync()

And it does work great for me when I tested

Working Example

like image 191
Tarun Lalwani Avatar answered Sep 30 '22 07:09

Tarun Lalwani