使用Python客户端API在Google Cloud Pub / Sub中运行同步拉动

我在 Python客户端API中找不到returnImmediately标志.
这有什么具体原因吗?
是否有另一种方法可以从Python中的订阅同步拉出排队的消息?
谷歌不提供这样的东西.但您可以通过实现自己的队列轻松解决此问题

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

当我测试时,它确实对我有用

Working Example

相关文章
相关标签/搜索