简体   繁体   English

如何使用PUBSUB作为触发器从Google Cloud函数返回JSON?

[英]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.

相关问题 “请求被中止,因为没有可用的实例” - Google Cloud Functions(PubSub 触发器) - "The request was aborted because there was no available instance" - Google Cloud Functions (PubSub trigger) 如何使用 Google Cloud Functions / Tasks / PubSub 进行批处理? - How to use Google Cloud Functions / Tasks / PubSub for Batch Processing? 谷歌云功能:返回有效 JSON - Google Cloud Functions: Return valid JSON 在一个项目中触发 Google Cloud function 从另一个项目发布到 Google PubSub - Trigger Google Cloud function in one project on publish in Google PubSub from another project 直接从谷歌存储读取 JSON 文件(使用 Cloud Functions) - Read JSON file directly from google storage (using Cloud Functions) 如何在谷歌云中保存一些规则或配置,然后调用它来处理云函数中的发布订阅主题数据? - How can I save some rules or config in google cloud and then call it to process a pubsub topic data in cloud functions? 如何通过Google-cloud-pubsub 0.28版的PublisherClient使用Pubsub仿真器 - How to use Pubsub Emulator with PublisherClient from google-cloud-pubsub version 0.28 如何从Google Cloud Functions发送gzipped json响应? - How to send gzipped json response from Google Cloud Functions? Google Cloud Dataflow - 从 PubSub 到 Parquet - Google Cloud Dataflow - From PubSub to Parquet 如何从 Google Cloud 函数返回 JSON - How to I return JSON from a Google Cloud Function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM