How to prevent Google Cloud Engine to run multiple concurrent instances?

131 Views Asked by At

Hi I'm deploying a very simple python code to Google App Engine just to test how Google App Engine works, that I'll later use for my real more complex code that I have tested locally. The problem is that Google App Engine logs is showing that I'm running multiple services / threads / instances / modules or whatever that name is... CONCURRENTLY

This is my code: main.py

import time
    
def executeSomething():
    #code here
    print("Hello world!")
    time.sleep(5)
    
while True:
    executeSomething()

and app.yaml

runtime: python38
service: default
handlers:
  - url: /
    script: auto
manual_scaling:
  instances: 1

I deploy using gcloud app deploy --stop-previous-version --version test-0-0-3

Then I tail the logs using gcloud app logs tail -s default

    2022-08-09 15:59:18 default[test-0-0-3]  Hello world!
    2022-08-09 15:59:18 default[test-0-0-3]  Hello world!
    2022-08-09 15:59:18 default[test-0-0-3]  Hello world!
    2022-08-09 15:59:18 default[test-0-0-3]  Hello world!
    2022-08-09 15:59:23 default[test-0-0-3]  Hello world!
    2022-08-09 15:59:23 default[test-0-0-3]  Hello world!
    2022-08-09 15:59:23 default[test-0-0-3]  Hello world!
    2022-08-09 15:59:23 default[test-0-0-3]  Hello world!
    2022-08-09 15:59:28 default[test-0-0-3]  Hello world!
    2022-08-09 15:59:28 default[test-0-0-3]  Hello world!
    2022-08-09 15:59:28 default[test-0-0-3]  Hello world!
    2022-08-09 15:59:28 default[test-0-0-3]  Hello world!
    2022-08-09 15:59:33 default[test-0-0-3]  Hello world!
    2022-08-09 15:59:33 default[test-0-0-3]  Hello world!
    2022-08-09 15:59:33 default[test-0-0-3]  Hello world!
    2022-08-09 15:59:33 default[test-0-0-3]  Hello world!

As you can see, there are 4 concurrent services running. Idk what is it called: services / threads / instances, but the problem is it's running several codes concurrently.

In my real code, I only want to run 1 service because I am afraid if I run multiple concurrently, it will create duplicates in the data I'm inserting to DB.

1

There are 1 best solutions below

2
minou On

From the comments, the OP wants to run a daily task with CRON jobs. In this case, the number of instances doesn't matter and the CRON job will run only once.

You will see multiple instances at deployment, but with Google App Engine standard, you should have 0 or 1 paid instance running at all times and you should be easily able to say within the free quota.

GAE is a good solution for this task.