使用python google-cloud-pubsub库,通过subscriber.acknowledge()确认消息不会确认我的消息。我的确认截止时间设置为30秒。
下面是我的代码:
from google.cloud import pubsub_v1
project_id = "$$$$"
subscription_name = "$$$$"
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_name)
response = subscriber.pull(subscription_path, max_messages=10, timeout=15)
for msg in response.received_messages:
subscriber.acknowledge(subscription=subscription_path, ack_ids=[msg.ack_id])使用google-cloud-pubsub==1.0.2
你知道我做错了什么吗?
发布于 2019-12-06 01:18:46
我建议您参考Synchronous Pull documentation,然后用Python语言运行示例代码来提取和确认消息:
from google.cloud import pubsub_v1
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)
NUM_MESSAGES = 3
response = subscriber.pull(subscription_path, max_messages=NUM_MESSAGES)
ack_ids = []
for received_message in response.received_messages:
print("Received: {}".format(received_message.message.data))
ack_ids.append(received_message.ack_id)
subscriber.acknowledge(subscription_path, ack_ids)
print('Received and acknowledged {} messages. Done.'.format(
len(response.received_messages)))我在您的代码中找不到ack_ids = []的定义(在开始在代码中使用它之前,您需要定义它)。如果您在运行这段代码时看到积极的结果,则可以假定您的代码中存在错误。你提供完整的代码了吗?
https://stackoverflow.com/questions/59197238
复制相似问题