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的主要内容,如果未能解决你的问题,请参考以下文章