Although the title is Flask, the following tutorial applies not only to Flask, but to other Python Web frameworks as well.
Flask is known to be a synchronous framework that processes requests in a single-process manner, and the Flask service blocks when too many people access it at the same time.
Just like when we buy train tickets, when there are more people buying train tickets, there will be a lot of people queuing, the queue will be very long, the corresponding waiting time will become very long!
Therefore, Flask, Django, Webpy and other frameworks come with poor performance web Server, can only be used for testing purposes, online distribution needs to choose wsGI Server with higher performance. The recommended deployment modes are as follows: Nginx + Gunicorn + Flask + Supervisor
Nginx: high-performance Web server + load balancing;
Gunicorn: High performance WSGI server;
Gevent: a library that turns Python synchronous code into an asynchronous coroutine;
Supervisor: a tool that monitors service processes.
Here’s a picture to give you a little intuition
Gunicorn
Gunicorn can specify multiple work processes, and you can choose from a variety of work modes. The default mode is sync. Other options include GEvent, Tronado, GThread, gaiohttp, etc.
Gevent is a greenlet-based library implemented using Python coroutines so that your Web services can be concurrent!
How is a high-performance Web service built?
Nginx
Nginx can actually only handle static resource requests, so what about dynamic requests? This requires the upstream module of Nginx to forward these requests, or reverse proxy. Nginx is mainly used for load balancing, and it can cache some dynamic content
Install nginx
The installation command is as follows:
sudo apt-get install nginx
After nginx is installed, you can use the following commands to enable and disable nginx
Sudo /etc/init.d/nginx restart //
Sudo /etc/init.d/nginx start Enable the function
Run the sudo /etc/init.d/nginx stop command to disable the function
Configure nginx
The Nginx configuration file is located
/usr/local/nginx/conf/nginx.conf
server {
listen 8080; Server_name localhost = 8080; Location / {proxy_pass http://127.0.0.1:9600; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_read_timeout 300; proxy_send_timeout 300; }Copy the code
}
Save the changes and restart nginx.
Supervisor
The installation supervisor
The command is as follows:
pip install supervisor
Initialize the configuration file:
echo_supervisord_conf > supervisor.conf
Modify the configuration file and add the corresponding configuration at the bottom of the configuration file
[include] Configure your own project
[program:project]
directory = /home/jerry/Code/project ; The startup directory of the program
command = /home/jerry/.virtualenvs/parsing/bin/gunicorn -w 4
-worker-class gevent-bind 0.0.0.0:9600 app:app; Start the command
numprocs=1 ; number of processes copies to start (def 1)
autostart = true ; It is also designed to run automatically when it is launched
startsecs = 1 ; If there is no abnormal exit after 1 second of startup, it is regarded as normal startup
autorestart = true ; The program automatically restarts after an abnormal exit
startretries = 3 ; The default value is 3
user = root ; Which user to boot with
redirect_stderr = true ; Redirect stderr to stdout, default false stdout_logFILe_maxBytes = 20MB; Stdout log file size, default 50MB
stdout_logfile_backups = 10 ; Stdout Number of backup log files
stdout_logfile=/home/jerry/Code/project/log/gunicorn.log ; The log log
stderr_logfile=/home/jerry/Code/project/log/gunicorn.error ; The error log
After editing, save and start the Supervisor. The command is the same as the gunicorn command, where -w refers to the number of processes in the service.
Basic commands
Start the Supervisor from the configuration file
supervisord -c supervisor.conf
View the Supervisor status
supervisorctl -c supervisor.conf status
Reload the configuration file after each change
supervisorctl -c supervisor.conf reload
Start specified/all Supervisor managed process
supervisorctl -c supervisor.conf start [all]|[appname]
Closes specified/all Supervisor managed program processes
supervisorctl -c supervisor.conf stop [all]|[appname]
That’s when you can access your app at http://127.0.0.1:8080!
To see how this works, you can test it out yourself by adding sleep to your code, or by writing a script yourself!