Deploying flash project based on gunicorn

Posted by esas_nefret on Thu, 30 Dec 2021 08:42:29 +0100

1. WSGI protocol

The Web framework focuses on how to generate HTML code, and the Web server is used to process and respond to HTTP requests. The communication between Web framework and Web server requires a set of interface protocols that both sides abide by. WSGI protocol is used to unify the two interfaces.

2. WSGI container

The commonly used WSGI containers include Gunicorn and uWSGI, but Gunicorn is started directly with a command without writing a configuration file. It is much easier than uWSGI, so I also choose Gunicorn as the container here.

3. Introduction to gunicorn

Gunicorn is a python Wsgi http server. It only supports running on Unix systems. It comes from the unicorn project of Ruby. Gunicorn uses the prefork master worker model (in gunicorn, master is called arbiter) to collaborate with various wsgi web frameworks.

4. gunicorn installation

Gunicorn installation is very simple. Use the command pip install gunicorn:

pip install gunicorn

It is generally used to use its asynchronous worker model, and the corresponding asynchronous module needs to be installed:

pip install greenlet # Must be installed to use asynchronous
pip install eventlet # Using eventlet workers
pip install gevent   # Using gevent workers

5. gunicorn use

Here is an example of using gunicorn to deploy a flash project. The use of flash framework here is not the focus of this article.

The following example is saved as app py

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

The parameters commonly used by gunicorn are as follows:

# Set the profile.
-c CONFIG, --config=CONFIG

# Set the port that the service needs to bind. HOST:PORT is recommended.
-b BIND, --bind=BIND

# Set the number of worker processes. It is recommended that each core of the server can be set with 2-4.
-w WORKERS, --workers=WORKERS

# Select the module to use for asynchronous operation.
-k MODULE

Enter your startup configuration in the shell, such as:

gunicorn -w 3 -b 127.0.0.1:8080 app:app
# Here, app: in the app, the first app is the package where the flash project instance is located, and the second app is the generated FLASH project instance

In this way, you can start the server when it runs normally.

6. Binding port

linux usually forbids binding to use ports below 1024, unless it is under root user permission. When using gunicorn, many people try to bind it to port 80 or 443 and find it invalid. If you want to bind to these ports, there are several common methods:

  • Forward using Nginx proxy.
  • sudo starts gunicorn.
  • Install additional programs.

7. End gunicorn service process

Use the ps -ef | grep gunicorn command to find all processes in gunicorn.

[root@VM_0_12_centos ~]# ps -ef | grep gunicorn
root     16843 23035  0 Oct14 ?        00:00:02 /root/Envs/myflask/bin/python3.6 /root/Envs/myflask/bin/gunicorn -w 3 -b 172.17.0.12:80 app:app
root     22445 23035  0 Oct04 ?        00:00:15 /root/Envs/myflask/bin/python3.6 /root/Envs/myflask/bin/gunicorn -w 3 -b 172.17.0.12:80 app:app
root     22581 23035  0 Oct11 ?        00:00:05 /root/Envs/myflask/bin/python3.6 /root/Envs/myflask/bin/gunicorn -w 3 -b 172.17.0.12:80 app:app
root     23035     1  0 Sep27 ?        00:04:11 /root/Envs/myflask/bin/python3.6 /root/Envs/myflask/bin/gunicorn -w 3 -b 172.17.0.12:80 app:app

Then use the kill -9 process ID command to kill the process. Note that we can find the main process and kill it, and the child process will end. In the above example, the main process number is 23035

[root@VM_0_12_centos ~]# kill -9 23035
[root@VM_0_12_centos ~]# ps -ef | grep gunicorn

After killing the process, wait a few seconds, and then use ps -ef | grep gunicorn to check. It is found that all gunicorn service processes have been killed.

[reference blog]:

Topics: Python