python Python:订阅GCP Pub / Sub

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Python:订阅GCP Pub / Sub相关的知识,希望对你有一定的参考价值。

from google.cloud import pubsub
subscriber = pubsub.SubscriberClient()

# Define the callback.
# Note that the callback is defined *before* the subscription is opened.
def callback(message):
    do_something_with(message)  # Replace this with your actual logic.
    message.ack()

# Open the subscription, passing the callback.
future = subscriber.subscribe(
    'projects/{project}/subscriptions/{subscription}',
    callback
)

# To block the thread you are in while messages are
# coming in the stream, use the result() method
try:
    future.result()
except Exception as ex:
    subscription.close()
    raise

# Finally, you can use cancel() to stop receiving messages.
future.cancel()

以上是关于python Python:订阅GCP Pub / Sub的主要内容,如果未能解决你的问题,请参考以下文章

为啥 GCP Pub/Sub 订阅无法确认消息?

GCP Pub/Sub 消息发送到创建主题之前存在的订阅

GCP Pub/Sub,如果已有活动订阅,您能否在新订阅上重播旧消息

确认后 GCP 消息保留在 Pub/Sub 中

GCP Pub/sub:使用 goroutine 让多个订阅者在一个应用程序中运行

如何在 GCP Pub/sub 中修复来自推送订阅的多条消息