Start a flask application in separate thread

Posted on

Question :

Start a flask application in separate thread

I’m currently developing a Python application on which I want to see real-time statistics. I wanted to use Flask in order to make it easy to use and to understand.

The issue is that my Flask server should start at the very beginning of my Python application and stop at the very end. It should look like this:

def main():
    """ My main application """
    from watcher.flask import app
    # watcher.flask define an app as in the Quickstart flask documentation.
    # See: # Starting the flask application


    app.stop() # Undefined, for the idea 

Because I need my application context (for the statistics), I can’t use a multiprocessing.Process. Then I was trying to use a threading.Thread, but it looks like Werkzeug doesn’t like it:

 * Running on
Exception in thread Flask Server:
Traceback (most recent call last):
  File "/usr/lib/python2.7/", line 810, in __bootstrap_inner
  File "/usr/lib/python2.7/", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File ".../develop-eggs/watcher.flask/src/watcher/flask/", line 14, in _run, port=PORT, debug=DEBUG)
  File ".../eggs/Flask-0.10.1-py2.7.egg/flask/", line 772, in run
    run_simple(host, port, self, **options)
  File ".../eggs/Werkzeug-0.7-py2.7.egg/werkzeug/", line 609, in run_simple
    run_with_reloader(inner, extra_files, reloader_interval)
  File ".../eggs/Werkzeug-0.7-py2.7.egg/werkzeug/", line 524, in run_with_reloader
    signal.signal(signal.SIGTERM, lambda *args: sys.exit(0))
ValueError: signal only works in main thread

How can I do that without running Flask in the main thread?

Answer #1:

You’re running Flask in debug mode, which enables the reloader (reloads the Flask server when your code changes).

Flask can run just fine in a separate thread, but the reloader expects to run in the main thread.

To solve your issue, you should either disable debug (app.debug = False), or disable the reloader (app.use_reloader=False).

Those can also be passed as arguments to, use_reloader=False).

Answered By: Thomas Orozco

Answer #2:

Updated answer for Python 3 that’s a bit simpler:

from flask import Flask                                                         
import threading

data = 'foo'
app = Flask(__name__)

def main():
    return data

if __name__ == "__main__":
Answered By: skeller88

Answer #3:

If you are looking for accessing iPython terminal in Flask
run your application in a separate thread. Try this example:

from flask import Flask                                                         
import thread

data = 'foo'
app = Flask(__name__)

def main():
    return data

def flaskThread():

if __name__ == "__main__":
    thread.start_new_thread(flaskThread, ())

(Run this file in iPython)

Answered By: Rashid Mv

Answer #4:

From the werkzeug documentation:

Shutting Down The Server

New in version 0.7.

Starting with Werkzeug 0.7 the development server provides a way to
shut down the server after a request. This currently only works with
Python 2.6 and later and will only work with the development server.
To initiate the shutdown you have to call a function named
‘werkzeug.server.shutdown’ in the WSGI environment:

def shutdown_server(environ):
    if not 'werkzeug.server.shutdown' in environ:
        raise RuntimeError('Not running the development server')
Answered By: Burhan Khalid

Leave a Reply

Your email address will not be published.