Configuring Flask for production

Ibrezm
2 min readJan 31, 2021

How many time have we though of deploying a flask to production boxes? Figured that flask internal server will not be able to handle it ? we wanted a light weight server for our ML models. will it restart on its own? how to deploy it as a service?

Well I too though of this and for lack of a complete tutorial here is what I have learned along the way. My pains and sweat and tears along with the vigorous google searches will be visible here and so pardon my misses , please let me know if their was a better way of doing things.

So Lets get started, Lets have a simple flask application (hello.py), I have excluded the items for virtual environment creation which is actually a very good practice for the sake of being concise

from flask import Flask
app = Flask(__name__)
@app.route(‘/’)
def index():
return ‘Index Page’
@app.route(‘/hello’)
def hello():
return ‘Hello, World’
if __name__ == “__main__”:
app.run()

Quickly to run we just export the variables and run as a python app and trigger the application

$ export FLASK_APP=hello.py
$ python -m flask run
* Running on http://127.0.0.1:5000/

Now we have the code but next question , how to deploy to production, the messaging in the flask clearly states this is for non prod. Also what if this shuts down due to errors ?

Waitress to rescue you

pip install waitress

The new code for waitress (hello.py)

from flask import Flask
from waitress import serve
app = Flask(__name__)
@app.route(‘/’)
def index():
return ‘Index Page’
@app.route(‘/hello’)
def hello():
return ‘Hello, World’
if __name__ == “__main__”:
#app.run() ##Replaced with below code to run it using waitress
serve(app, host=’0.0.0.0', port=8002)

Now to quickly run it

waitress-serve hello:app

So server issue sorted lets tackle the service problem, the below steps specify to deploy it as a service on that machine(linux only , sorry windows guys)

# /etc/systemd/system/hello_app.service
[Unit]
Description=My flask app
After=network.target
[Service]
Type=simple
User=<user>
Environment=FLASK_APP=hello
WorkingDirectory=/home/<user>
ExecStart=/path/to/waitress-serve — listen=0.0.0.0:8000 hello:app
Restart=always
[Install]
WantedBy=multi-user.target

Well most of the items are done once you have placed this in system directory now just start the service

sudo systemctl enable hello_app.service
sudo systemctl start hello_app.service

There you have it all items sorted. Next steps … to take this app to docker and beyond .. so stay tuned.

--

--