Reputation: 3520
I've been running a Python3.6 script on my laptop and now I'd like to deploy it to GCP App Engine. I've used their tutorials and others to convert the script into a WSGI. However, I'm having trouble getting a background task to run constantly and also be able to use the route to see the output.
Here's the simple sample. Using this I can see the / route output message Hello World!
from flask import Flask
app = Flask(__name__)
@app.route('/')
def main():
"""Say hello"""
return 'Hello, world!'
if __name__ == '__main__':
app.run()
If I add a while loop, the loop works but then the route no longer functions. Which makes sense, I just don't know the syntax to have the while statement run as a background task. I've seen CRON examples, but this is something that runs constantly--not every X minutes.
from flask import Flask
app = Flask(__name__)
@app.route('/')
counter = 0
while True:
counter += counter
print(counter)
def main():
"""Say hello"""
return 'Hello, world! ' + str(counter)
if __name__ == '__main__':
app.run()
Update: Should I use Flask-APScheduler for this?: https://github.com/viniciuschiele/flask-apscheduler
Upvotes: 3
Views: 4791
Reputation: 688
I know this question is old, but I came across it while trying to search for ideas for a similar but a little more complex problem and thought I'd give this a shot.
I start a thread in the background which does the the counting when the Flask app starts and then read the current value in the route handler.
from flask import Flask
from threading import Thread
app = Flask(__name__)
index_counter = 0
def counter():
while True:
global index_counter
index_counter += 1
def start_counter_thread():
counter_thread = Thread(target=counter, daemon=True)
counter_thread.start()
start_counter_thread()
@app.route('/')
def main():
"""Say hello"""
return "Hello World: %s" % index_counter
if __name__ == '__main__':
# This didn't work like I expected with reload mode on because it looks like
# a subprocess is started which effectively has two separate copies of
# this app running. So the system was generating at least two separate
# sequences of numbers. I'm probably doing something wrong here.
app.run(processes=10, threaded=False, use_reloader=False)
Upvotes: 1
Reputation: 299
One way to approach this would be to create two routes - one that starts the counter and and that returns the count itself. Note, I've added "threaded=True" to allow multiple calls to be made.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def main():
"""Say hello"""
global index_counter
print(index_counter)
return "Hello World: %s" % str(index_counter)
@app.route('/counter')
def counter():
global index_counter
index_counter = 0
while True:
index_counter += 1
if __name__ == '__main__':
app.run(threaded=True)
Now, you can first invoke:
This will start the counter.
And then at any point you can invoke: http://localhost
And the should print out the counter value.
Upvotes: 3