2 回答
TA贡献1995条经验 获得超2个赞
非常感谢您的所有回复。我刚刚使用下面的代码片段示例解决了这个问题
def receive_messages_synchronously(project, subscription_name):
"""Pulling messages synchronously."""
# [START pubsub_subscriber_sync_pull]
# project = "Your Google Cloud Project ID"
# subscription_name = "Your Pubsub subscription name"
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(
project, subscription_name)
# Builds a pull request with a specific number of messages to return.
# `return_immediately` is set to False so that the system waits (for a
# bounded amount of time) until at lease one message is available.
response = subscriber.pull(
subscription_path,
max_messages=3,
return_immediately=False)
ack_ids = []
for received_message in response.received_messages:
print("Received: {}".format(received_message.message.data))
ack_ids.append(received_message.ack_id)
# Acknowledges the received messages so they will not be sent again.
subscriber.acknowledge(subscription_path, ack_ids)
# [END pubsub_subscriber_sync_pull]
原因是创建的订阅使用拉取请求。我猜使用的回调方法概念主要是为了“推送”,这可能是因为我没有提供端点和令牌来发布消息。希望我的猜测是正确的。也让我知道你的看法。
TA贡献1811条经验 获得超6个赞
这可能是由于以下两个因素之一造成的:
向 AutoML API 发送请求时使用的凭据无效 - pubsub 很可能在其他上下文中执行并且无法获取默认凭据
无效的模型资源名称(确保它是正确的) - 它应该是这样的:“projects/12423534/locations/us-central1/models/23432423”
添加回答
举报