[英]How to return a JSON from Google Cloud functions using PUBSUB as trigger?
I am publishing to topics using Python pubsub client, and there is a cloud function set up that is triggered by pubsub messages. 我使用Python pubsub客户端发布主题,并且有一个由pubsub消息触发的云功能设置。 I can trigger the function properly and generate the desired return value but I cant figure to return that value to publisher/client side.
我可以正确触发函数并生成所需的返回值,但我无法将该值返回给发布者/客户端。 Thanks everyone!
感谢大家!
client code: 客户代码:
def call_getTime():
message_future = publisher.publish(topic_path,
data=data,
)
message_future.add_done_callback(callback)
print(message_future.result())
def callback(message_future):
if message_future.exception(timeout=30):
print('Publishing message threw an Exception {}.'.format(
message_future.exception()))
else:
print(message_future.result())
Cloud Function: 云功能:
def getTime(data, context):
r = {'time': time.time()}
return flask.jsonify(r)
Pubsub functions don't "return" messages. Pubsub函数不会“返回”消息。 They just consume messages, and they don't normally care where the message came from.
他们只是消费消息,他们通常不关心消息的来源。 It's not a two-way form of communication.
这不是一种双向沟通方式。
If you want two-way communications, use an HTTP trigger instead. 如果您想要双向通信,请改用HTTP触发器。 You can send a message back in the body of the HTTP response.
您可以在HTTP响应的正文中发回消息。
If you can't use HTTP for whatever reason and must stick with pubsub, consider publishing another message to another topic, and arrange for the sender to receive that message on that other topic. 如果由于某种原因无法使用HTTP并且必须坚持使用pubsub,请考虑将另一条消息发布到另一个主题,并安排发件人在该另一主题上接收该消息。 Or use some sort of webhook to notify someone that the message was handled.
或使用某种webhook通知某人该消息已被处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.