我正在尝试使用相同的代码将两个不同的 google pub/sub 订阅者设置为不同的订阅。为了描绘出更好的画面,假设我有主题 1 和主题 2。然后我有订阅topic1的subscription1和订阅topic2的subscription2。然后我有订阅者 1,它链接到订阅 1,订阅者 2 链接到订阅 2。我的问题是如何在同一应用程序中使用订阅者 1 和订阅者 2。我仅针对 1 个订阅者的示例是(来自文档):
project_id = "my-project-id"
subscription_id = "subscription1"
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)
streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
logging.info("Listening for messages on {}..\n".format(subscription_path))
# Wrap subscriber in a 'with' block to automatically call close() when done.
with subscriber:
try:
# When `timeout` is not set, result() will block indefinitely,
# unless an exception is encountered first.
streaming_pull_future.result()
except TimeoutError:
streaming_pull_future.cancel()
Run Code Online (Sandbox Code Playgroud)
如何将 subscription2 添加到其中,以便我的 python …