Akshar Patel
Akshar Patel

Reputation: 9388

Always Running Python Script on Google Cloud

I want to host a python script on Google Cloud which would run indefinitely.

Which Google Cloud Products can be used to achieve this reliably and economically?

is this possible on AppEngine?

Upvotes: 4

Views: 1951

Answers (2)

Gurjot Singh
Gurjot Singh

Reputation: 105

It is possible on appengine, try shifting to instance type backend in the app.yaml
You can learn more about backend/frontend instances here

Upvotes: 2

Kolban
Kolban

Reputation: 15276

Within GCP there are a number of ways to run processing logic. We have App Engine, Functions, Compute Engine, GKE, Cloud Run and more. Of these, Compute Engine and GKE provide the lowest level control meaning that you can orchestrate code to run 100% of the time ... and basically do anything you want. Based on your story, it sounds like a Compute Engine would be what you want. When you create a Compute Engine, you are basically given a Virtual Machine (commonly Linux) and you simply run what ever logic / applications you want within that environment. Google charges you for the duration that the Compute Engine runs. If you have an always running explicit Python app, then you will be billed 24x7 irrespective of whether or not traffic is actually arriving.

Other capabilities that you are likely going to want to consider are Cloud Functions. A Cloud Function is logic that runs only when there is an active request to be processed. It is GCP's responsibility to passively listen for new requests without charging you for that service. When a request arrives, your logic is executed and you are billed only for the duration that your logic ACTUALLY executes. Cloud Run is similar to Cloud Functions but the logic is hosted in a Kubernetes environment as opposed to native.

For Cloud Function, incoming requests can arrive either via REST requests or via GCP Pub/Sub. In your description, neither of these apply as you are receiving data via MQTT. This is where Google IOT Core comes into play. Google IoT core is a full suite environment for managing IoT devices. Primary among this story is the ability to passively listen for incoming MQTT requests. When an authorized IoT device sends in an MQTT request, the Google IoT core automatically forwards the content of the MQTT request via GCP Pub/Sub. This then decouples the mechanics of the arriving IoT message from how it will be eventually processed. Since the MQTT message has now been published to GCP Pub/Sub, this can then be used as a trigger for a Google Cloud Function.

Following all these parts ... one possibility to examine which may result in the cheapest and more dynamically scalable solution would be:

IoT Device --- MQTT ---> IoT Core --- Pub/Sub ---> Cloud Function

This story has basically changed the paradigm from "How do I actively listen for requests in my code in an indefinite manner" to "How do I have my code executed when a request arrives".

Upvotes: 3

Related Questions