Although the title is Flask, the following tutorial applies not only to Flask, but also to other Python Web frameworks, so give it a thumbup!

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. Recommended deployment mode: Nginx + Gunicorn + Flask + Supervisor

Where each service represents the following meanings:

  • 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!

I’ve written about Gunicorn before. For details, check it out!

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
Copy the code

After nginx is installed, you can use the following commands to enable and disable nginx

D /nginx restart sudo /etc/init.d/nginx start Sudo /etc/init.d/nginx stop Stop the serviceCopy the code

Configure nginx

Nginx configuration file located in/usr/local/Nginx/conf/Nginx. Conf

server {
    listen     8080; # listen on port 8080
    server_name localhost; # configure domain name

    # Dynamic request forward to port 9600 (gunicorn):
    location / {
        proxy_pass   http://127.0. 01.: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
Copy the code

Initialize the configuration file:

echo_supervisord_conf > supervisor.conf
Copy the code

Modify the configuration file and add the corresponding configuration at the bottom of the configuration file

[program:project] directory = /home/jerry/Code/project; Application startup directory command = / home/jerry /. Virtualenvs/parsing/bin/gunicorn - 4 - worker w - class gevent - bind 0.0.0.0:9600 app: app; Start the numprocs=1 command. number of processes copies to start (def 1) autostart = true ; Startsecs = 1 when it is running in the container; If there is no abnormal exit after 1 second, the system considers that the system is successfully started. Startretries = 3; The default value is 3 user = root. Which user starts redirect_stderr = true; Redirect stderr to stdout, default false stdout_logFILe_maxBytes = 20MB; Stdout logfile size. Default value: 50MB stdout_logfile_backups = 10; Stdout number log file backup stdout_logfile = / home/jerry/Code/project/log/gunicorn log; The log log stderr_logfile = / home/jerry/Code/project/log/gunicorn error; The error logCopy the code

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 
Copy the code

View the Supervisor status

supervisorctl -c supervisor.conf status 
Copy the code

Reload the configuration file after each change

supervisorctl -c supervisor.conf reload
Copy the code

Start specified/all Supervisor managed process

supervisorctl -c supervisor.conf start [all]|[appname]
Copy the code

Closes specified/all Supervisor managed program processes

supervisorctl -c supervisor.conf stop [all]|[appname]
Copy the code

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!

Recommended reading

How is a high-performance Web service built?

Read the relationship between the Python Web framework and the Web server

Flask blueprint mechanism and application

Methods of displaying Pyecharts chart data dynamically by Flask