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)
Advertisement
Privacy Settings

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.