Run your Flask App in Production...
Introduction:
Well, we have seen how to create our first Flask Application which is quite easy to set up. If you haven't done it yet then please check our Hello World article
Why do we need something extra?
Of course, Flask does come with an inbuilt server that helps during development. But it is not recommended to use it in the Flask documentation since While lightweight and easy to use, Flask’s built-in server is not suitable for production as it doesn’t scale well.
So how do I run my Flask App then?
We need a production WSGI server. A WSGI Server is something that basically implements a mechanism to enable us to run our Python Application to accept and respond to HTTP requests. A few WSGI servers that are generally used by developers worldwide to run their Flask Applications are gunicorn and uwsgi. We will see to run our sample application through both of them.
Gunicorn WSGI Server:
Gunicorn Green Unicorn
is a Python WSGI HTTP Server for UNIX systems.
uWSGI Server:
uWSGI is a software application that aims to develop a full stack for building hosting services.`
Let's fire up our VIrtualEnv first:
flask-india@dev-pc:~/basic-flask-app$ . app-venv/bin/activate
(app-venv) flask-india@dev-pc:~/basic-flask-app
Next, let's install gunicorn first.
(app-venv) flask-india@dev-pc:~/basic-flask-app$ pip install gunicorn
Collecting gunicorn
Using cached gunicorn-20.1.0-py3-none-any.whl (79 kB)
Requirement already satisfied: setuptools>=3.0 in ./app-venv/lib/python3.8/site-packages (from gunicorn) (44.0.0)
Installing collected packages: gunicorn
Successfully installed gunicorn-20.1.0
You can check if gunicorn is installed using the command below.
(app-venv) flask-india@dev-pc:~/basic-flask-app$ pip freeze | grep gunicorn
gunicorn==20.1.0
(app-venv) flask-india@dev-pc:~/basic-flask-app$
Run your Flask App created using the previous article with the command below.
(app-venv) flask-india@dev-pc:~/basic-flask-app$ gunicorn app:app
[2022-06-05 23:01:18 +0530] [7858] [INFO] Starting gunicorn 20.1.0
[2022-06-05 23:01:18 +0530] [7858] [INFO] Listening at: http://127.0.0.1:8000 (7858)
[2022-06-05 23:01:18 +0530] [7858] [INFO] Using worker: sync
[2022-06-05 23:01:18 +0530] [7861] [INFO] Booting worker with pid: 7861
You can now test your app by doing a simple curl request to it. Notice the default port of 8000 while you run gunicorn, which you can customize it later.
flask-india@dev-pc:~/basic-flask-app$ curl http://localhost:8000
Hello World, Flask Dev here...flask-india@dev-pc:~basic-flask-app$
Press Ctrl+C to terminate the gunicorn process and shut down the app.
[2022-06-05 23:01:36 +0530] [7858] [INFO] Handling signal: winch
^C[2022-06-05 23:08:25 +0530] [7858] [INFO] Handling signal: int
[2022-06-05 23:08:26 +0530] [7861] [INFO] Worker exiting (pid: 7861)
[2022-06-05 23:08:26 +0530] [7858] [INFO] Shutting down: Master
(app-venv) flask-india@dev-pc:~/basic-flask-app$
Next, let's install uwsgi.
(app-venv) flask-india@dev-pc:~/basic-flask-app$ pip install uwsgi
Collecting uwsgi
Downloading uwsgi-2.0.20.tar.gz (804 kB)
|████████████████████████████████| 804 kB 4.8 MB/s
Building wheels for collected packages: uwsgi
Building wheel for uwsgi (setup.py) ... done
Created wheel for uwsgi: filename=uWSGI-2.0.20-cp38-cp38-linux_x86_64.whl size=515501 sha256=20685bb1c8dd3a9320bfdb80ca4666e4ad8e9a5087ee26a0fb1da80f43f41138
Stored in directory: /home/chinmay/.cache/pip/wheels/cb/4d/54/002a215100a8c4e125654600f0a4304dae742442ddc4ba6ea1
Successfully built uwsgi
Installing collected packages: uwsgi
Successfully installed uwsgi-2.0.20
(app-venv) flask-india@dev-pc:~/basic-flask-app$
You can check if uWSGI is installed using the command below.
(app-venv) flask-india@dev-pc:~/basic-flask-app$ pip freeze | grep uWSGI
uWSGI==2.0.20
(app-venv) flask-india@dev-pc:~/basic-flask-app$
Run your Flask App created using the previous article with the command below.
(app-venv) flask-india@dev-pc:~/basic-flask-app$ uwsgi --socket 0.0.0.0:5000 --protocol=http -w app:app
*** Starting uWSGI 2.0.20 (64bit) on [Sun Jun 5 23:20:58 2022] ***
compiled with version: 9.4.0 on 05 June 2022 17:41:44
os: Linux-5.13.0-44-generic #49~20.04.1-Ubuntu SMP Wed May 18 18:44:28 UTC 2022
nodename: dev-pc
machine: x86_64
clock source: unix
detected number of CPU cores: 8
current working directory: /basic-flask-app
detected binary path: /basic-flask-app/app-venv/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 62738
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address --protocol=http fd 3
Python version: 3.8.10 (default, Mar 15 2022, 12:22:08) [GCC 9.4.0]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x5588ea4b2530
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72904 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x5588ea4b2530 pid: 9294 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 9294, cores: 1)
You can now test your app by doing a simple curl request to it. Notice the port of 5000 while you run uWSGI, which you can customize it later.
flask-india@dev-pc:~/basic-flask-app$ curl http://localhost:5000
Hello World, Flask Dev here...flask-india@dev-pc:~basic-flask-app$
Press Ctrl+C to terminate the uWSGI process and shut down the app.
(app-venv) flask-india@dev-pc:~/basic-flask-app$
Conclusion
This completes a quick overview of running our Flask App using Gunicorn and uWSGI.
Subscribe to my newsletter
Read articles from Flask India directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Flask India
Flask India
We are a bunch of Developers who started to mentor beginners who started using Python and Flask in general in the Flask India Telegram Group. So this blog is a way to give it back to the community from where we have received guidance when we started as beginners.