【发布时间】:2021-08-20 05:56:42
【问题描述】:
GCP 为 Pub/Sub 提供 REST API:https://cloud.google.com/pubsub/docs/reference/rest
Python API 允许用户如下拉取消息,使用 Python 库https://googleapis.dev/python/pubsub/latest/index.html
import os
from google.cloud import pubsub_v1
topic_name = 'projects/{project_id}/topics/{topic}'.format(
project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
topic='MY_TOPIC_NAME', # Set this to something appropriate.
)
subscription_name = 'projects/{project_id}/subscriptions/{sub}'.format(
project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
sub='MY_SUBSCRIPTION_NAME', # Set this to something appropriate.
)
def callback(message):
print(message.data)
message.ack()
with pubsub_v1.SubscriberClient() as subscriber:
subscriber.create_subscription(
name=subscription_name, topic=topic_name)
future = subscriber.subscribe(subscription_name, callback)
try:
future.result()
except KeyboardInterrupt:
future.cancel()
但是用户必须使用这个库吗?
我想使用不同的 HTTP 客户端通过使用 Pub/Sub API 的 GET 请求来解析这些消息。原则上,这应该通过 requests 库之类的东西来完成,使用 get 方法:
import requests
x = requests.get(END_POINT)
print(x.status_code)
但是,这是否适用于来自 Google Pub/Sub 的流式 GET 请求?
【问题讨论】:
标签: python http get client google-cloud-pubsub