使用Python客户端API在Google Cloud Pub/Sub中运行同步提取 [英] Run synchronous pull in Google Cloud Pub/Sub with the Python client API

查看:138
本文介绍了使用Python客户端API在Google Cloud Pub/Sub中运行同步提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python客户端API中找不到 returnImmediately 标志. 有什么具体原因吗? 还有另一种方法可以从Python的订阅中同步提取排队的消息吗?

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?

推荐答案

Google未提供此类信息.但是您可以通过实现自己的队列来轻松解决此问题

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

这篇关于使用Python客户端API在Google Cloud Pub/Sub中运行同步提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆