Nugget Post: Python Flask framework and multiprocessing

When using the excellent Flask framework in conjunction with the python multiprocessing module , you may notice that the inbuilt Flask webserver will try to start up twice, leaving you with two webserver processing, each trying to bind to the same port. Needless to say this results in unexpected errors. Apparently this is a problem with the Flask auto-reload feature, which is used during development to automatically restart the webserver when changes in code is detected, in order to serve up the new code without requiring a manual restart. In the guide, the “app.run()” call is always placed within an “if __name__ == ‘__main__’” condition, since the reloader is set to on by default. When using multiprocessing, this condition will result in false, so we have to instead disable the Flas autoreload when using it in a function like so:

     def startWebserver():
          app.run(debug=True, use_reloader=False)