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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With