Skip to content
Advertisement

How to trigger async job from a Cloud Function

I have a Cloud Function (Python) which is triggered by http from web client, it has to calculate something and respond FAST. I would like to save the http request parameters into a database (for analytics).

If i just initiate a WRITE to my postgresql, the function will have to wait for it, and it will be slower.

Using PubSub, the Function also need to publish and wait for respond (Docs example) :

   # Publishes a message
    try:
        publish_future = publisher.publish(topic_path, data=message_bytes)
        publish_future.result()  # Verify the publish succeeded
        return 'Message published.'

Google does not offer a solution which trigger in the background automatically a pubSub when a http function is called.

How can I take information from my function and save it to my DB (any DB) without affecting the function execution at all.

def cloud_func(request):

      request_json = request.get_json()
        //save to db async without waiting for respond
        //calculate my stuff..
        return (result, 200, headers) //to client

Advertisement

Answer

If you use a Cloud Functions trigger in HTTP, I recommend you to migrate to Cloud Run, and to activate the always on CPU parameter.

That parameter is designed exactly for that: continue a process in background even outside a request handling context


EDIT 1

You can also imagine an async mechanism by staying on Cloud Functions. The sync function get the data from the user, publish a message in PubSub and answer to the user.

The PubSub message publication is very fast, and won’t take too much time.

Then write another function, that listen the PubSub topic and save the message data into your database. Because that function is async, you are not time constraints.

Advertisement