Python

Using Gunicorn with Flask

Gunicorn has a reload mode which can restart the server if any of the dependent files change on the disk. Let’s see how this works for the Flask microservice in the hasura/hello-python-flask quickstart. The directory structure is as shown below:

.
├── Dockerfile
├── k8s.yaml
├── conf
│   └── gunicorn_config.py
└── src
    ├── auth.py
    ├── __init__.py
    ├── requirements.txt
    └── server.py

Executing gunicorn --reload src:app here will start gunicorn in reload mode. It will restart the server for any change in the python files. Here, we have kept all the configuration in gunicorn_config.py. Hence, we set the reload parameter in this file:

reload = True

Let’s look at the dockerfile now:

# Step 1: use a base python image
FROM python:3

# Step 2: install requirements
COPY src/requirements.txt /tmp/requirements.txt
RUN pip3 install -r /tmp/requirements.txt

# Step 3: set /app as working directory
WORKDIR /app

# Step 4: copy current directory to /app
COPY . /app

# Step 5: run gunicorn server
# port is configured through the gunicorn config file
CMD ["gunicorn", "--config", "./conf/gunicorn_config.py", "src:app"]

As per step 4, the current microservice directory is copied into /app into the container image. So, after setting the reload flag in either step 5 or in gunicorn_conf.py (note that the conf file takes precedence over command line flags), you can execute the following command to sync the src directory (assuming the microservice is called api):

$ hasura microservice sync api microservices/api/src:/app/src

Keep the command running in a terminal. Open the microservice in a browser, using another terminal window:

$ hasura microservice open api

Make some changes to server.py and save. Refresh the browser and voila! You can see that it changes live in the browser tab.