Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a background thread to flask?

Tags:

python

rest

flask

I'm busy writing a small game server to try out flask. The game exposes an API via REST to users. It's easy for users to perform actions and query data, however I'd like to service the "game world" outside the app.run() loop to update game entities, etc. Given that Flask is so cleanly implemented, I'd like to see if there's a Flask way to do this.

like image 524
Marinus Avatar asked Jan 17 '13 17:01

Marinus


People also ask

How do I run a flask in the background?

Make sure, you close the terminal and not press Ctrl + C. This will allow it to run in background even when you log out. To stop it from running , ssh in to the pi again and run ps -ef |grep nohup and kill -9 XXXXX where XXXX is the pid you will get ps command.

How do I run a flask in another thread?

To start a Python Flask application in separate thread, we set the use_reloader to False when we call app. run . And then we create a Thread instance with the Flask app by setting the function that calls app. run as the value of the target argument.

What is background thread?

In this example, the callback is executed in the calling thread, which is a background thread. This means that you cannot modify or communicate directly with the UI layer until you switch back to the main thread.

Does flask use multiple threads?

As of Flask 1.0, flask server is multi-threaded by default. Each new request is handled in a new thread. This is a simple Flask application using default settings.


1 Answers

Your additional threads must be initiated from the same app that is called by the WSGI server.

The example below creates a background thread that executes every 5 seconds and manipulates data structures that are also available to Flask routed functions.

import threading import atexit from flask import Flask  POOL_TIME = 5 #Seconds      # variables that are accessible from anywhere commonDataStruct = {} # lock to control access to variable dataLock = threading.Lock() # thread handler yourThread = threading.Thread()  def create_app():     app = Flask(__name__)      def interrupt():         global yourThread         yourThread.cancel()      def doStuff():         global commonDataStruct         global yourThread         with dataLock:             pass             # Do your stuff with commonDataStruct Here          # Set the next thread to happen         yourThread = threading.Timer(POOL_TIME, doStuff, ())         yourThread.start()         def doStuffStart():         # Do initialisation stuff here         global yourThread         # Create your thread         yourThread = threading.Timer(POOL_TIME, doStuff, ())         yourThread.start()      # Initiate     doStuffStart()     # When you kill Flask (SIGTERM), clear the trigger for the next thread     atexit.register(interrupt)     return app  app = create_app()           

Call it from Gunicorn with something like this:

gunicorn -b 0.0.0.0:5000 --log-config log.conf --pid=app.pid myfile:app 
like image 169
caio Avatar answered Sep 21 '22 02:09

caio