LearnerJS
LearnerJS

Reputation: 299

How do I invoke another cloud function from one cloud function in Python? (Asynchronously-I don't need the response back.)

PS: I was doing it synchronously by using the following in my cloud function:

name = "projects/project1/locations/us-central1/functions/test3"
body = {
'data': '{ "message": "Function Invoked Successfully"}'
}
result = self.google_api_service.projects().locations().functions().call(name=name, body=body).execute()
#google_api_service has it's own functions
print(result)

Help me out to do it asynchronously! Also please do not suggest Pub/Sub. Is there any other workaround in python?

Upvotes: 2

Views: 1622

Answers (1)

Rafael Lemos
Rafael Lemos

Reputation: 5819

There are two parts to answer your question:

  1. In order to make an HTTP call to a separate function you have to setup an authorization token for the request. As you can see in the documentation you have to first:

Grant the Cloud Functions Invoker (roles/cloudfunctions.invoker) role to the calling function identity on the receiving function. By default, this identity is [email protected]

And then in the calling function you have to:

  • Create a Google-signed OAuth ID token with the audience (aud) set to the URL of the receiving function.
  • Include the ID token in an Authorization: Bearer ID_TOKEN header in the request to the function.

NOTE: You can follow the code example in the same documentation link on how to include it.

  1. in order for the call to be async, since you don't care for the result, you can call do it by using multiprocessing pool, which could look like this:
import multiprocessing
import requests
    
p = multiprocessing.Pool(processes = 1)
p.apply_async(requests.get, ['YOUR_FUNCTION_URL'])

Upvotes: 1

Related Questions