Avoiding GCF anti-patterns part 5: How to run background processes correctly in Python
Sara Ford
Senior Developer Relations Engineer
Martin Skoviera
Technical Solutions Engineer
Editor's note: Over the past several weeks, we've posted a series of blog posts focusing on best practices for writing Google Cloud Functions based on common questions or misconceptions as seen by the Support team. We refer to these as "anti-patterns" and offer you ways to avoid them. This article is the fifth post in the series.
Scenario
You see finished with status: 'timeout'
in the logs before a background process has completed in your Python Function.
Most common root issue
Although this timeout error can happen for Functions using any runtime, we most often see this issue occur when Python developers try to use os.fork()
or multiprocessing.Process()
in their Cloud Function.
Why you should try to avoid async work in a Function:
A background task started by a Cloud Function is not guaranteed to complete. As soon as the Functions completes, e.g. the Function returns or a timeout error occurs, the Function instance can be terminated at any time. You can read more about the Function execution timeline in the documentation.
We often see customers test their functions locally where these execution timeouts do not exist. Additionally, customers' local machines may be more powerful than what they have provisioned for their Cloud Functions. Customers may see these multiprocessing scenarios working locally and therefore assume their code will work in the same way in the Cloud Function instance.
For Python developers who require such async operations, we suggest using Cloud Tasks Service instead to schedule the background operation. See example below.
Using Cloud Tasks in a Python Cloud Function
The following Function demonstrates how you can use Cloud Tasks to schedule an async operation. This example shows a Cloud Function (named "create_task
") that creates a Cloud Task to invoke another Cloud Function that will run the background task. You can learn more about creating HTTP target tasks.Other helpful tips
Although this tutorial is written for Node.js, it walks you through creating a Cloud Task queue and setting up a service account that will invoke the Function from Cloud Task. By specifying a service account for the Task, you can use an authenticated Function.
If you're using a different service account to invoke the Function (rather than your Function's identity), you need to verify that the service account has the Cloud Functions Invoker role `roles/cloudfunctions.invoker`.
If you're using a different service account for your "create_task" Function's identity than the default, you need to verify that the service account has permissions to create Tasks. It will need the Cloud Tasks Enqueuer role `roles/cloudtasks.enqueuer`.