【问题标题】:Google Pub Sub serial task processing with Python Client使用 Python 客户端处理 Google Pub Sub 串行任务
【发布时间】:2018-10-24 07:07:23
【问题描述】:

我已经使用 Google Pubsub 设置了一个消息队列。一切正常。唯一的问题是所有任务都是同时运行的(好的,目前只有 3 个任务)。由于它们在目标服务器上的重量相当大,因此存在一些问题。

我的解决方案是处理一项任务,等到它完成并再次运行它。不幸的是,我在 Python 库中没有找到任何东西来设置 MaxProcessing。

我该怎么做?

【问题讨论】:

  • 嗨拉普斯利。恕我直言,我相信使用 Google Python PubSub API(相当恼人)无法控制消息以便“一次一个”地提取它们。前段时间我尝试过这样做,并测试了几种不同的方法,但无法让它发挥作用,但我应该提醒我,我对 Python 是个严格的业余爱好者。我对此进行了扩展,并在回复here 中提供了可能有帮助的链接。祝你好运!

标签: python google-cloud-platform google-cloud-pubsub


【解决方案1】:

您可以按照“消息流控制”小节中的说明限制发送给您的客户端的未完成消息的数量。我在下面复制了 python 的最小示例。 https://cloud.google.com/pubsub/docs/pull#subscriber-flow-control-python

from google.cloud import pubsub_v1

# TODO project_id = "Your Google Cloud Project ID"

# TODO subscription_name = "Your Pub/Sub subscription name"

subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(
    project_id, subscription_name)

def callback(message):
    print('Received message: {}'.format(message.data))
    message.ack()

# TODO max_messages_outstanding = 1

# Limit the subscriber to only have ten outstanding messages at a time.
flow_control = pubsub_v1.types.FlowControl(
    max_messages=max_messages_outstanding)
subscriber.subscribe(
    subscription_path, callback=callback, flow_control=flow_control)

-丹尼尔

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 2021-04-21
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    相关资源
    最近更新 更多