nginx+uwsgi+flask高并发配置

发布于 2021-02-26  277 次阅读


python的并发性能很尴尬,java随便一跑上万QPS,而python单进程最多只测试到过800QPS左右 (无业务逻辑)

uwsgi 官方文档:https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html

测试QPS大概: uwsgi 1 process 100 QPS

安装nginx以及uwsgi

sudo apt-get install uwsgi uwsgi-plugin-python3

新建uwsgi.ini配置,用于uwsgi启动

[uwsgi]
 
home = /data/anaconda3      # python env目录
# uWSGI 的监听端口
socket          = 127.0.0.1:5001
# 项目根目录
chdir           = /home/root/xxx
# Flask 项目的启动文件
wsgi-file       = run.py
# 程序内启用的application变量名
callable        = app 
# 进程数量

processes = 12   # 并发处理进程数
listen = 4096  # 并发的socket 连接数。默认为100。优化需要根据系统配置
vacuum          = true
master          = true
plugins = python3   # 使用上面安装的uwsgi-plugin-python3
enable-threads = true
daemonize = /data/website/log/uwsgi.log
uwsgi通过 xxx.ini 启动后会在相同目录下生成一个 xxx.pid 的文件,
里面只有一行内容是 uwsgi的主进程的进程号。
因为上面配置文件是开启主进程的,所以这里直接这样结束就行
sudo pkill -f uwsgi -9  
# 启动
uwsgi --ini uwsgi.ini
# 重启            
uwsgi --reload uwsgi.pid  
# 关闭        
uwsgi --stop uwsgi.pid            

yourwebsite.conf 站点配置

server {

    listen 5050;
    
    server_name _;
    charset utf-8;
 
    client_max_body_size 75M;

    location / { 
 
        include uwsgi_params;                       # 导入uwsgi配置 
    
        uwsgi_pass 127.0.0.1:5001;                  # 转发端口,需要和uwsgi配置当中的监听端口一致
        uwsgi_param UWSGI_PYTHON /usr/bin/python3;  # Python解释器所在的路径,如果有虚拟环境可将路径设置为虚拟环境
        uwsgi_param UWSGI_CHDIR /home/root/xxx;         # 项目根目录
        uwsgi_param UWSGI_SCRIPT run:app;           # 项目的主程序,比如你测试用run.py文件,文件中app = Flask(__name__),那么这里就填run:app 
    }   
}

修改/etc/nginx/nginx.conf,开启epoll

events {
         use epoll;
         worker_connections 10240;
         multi_accept on;
}
ulimit -n 20480
# 查看是否生效
ulimit -a

flask配置,开启全局gevent

from gevent import monkey
from gevent.pywsgi import WSGIServer
monkey.patch_all()

#xxxxxxxxxxxxxxxxxxxx

if __name__ == "__main__":

    WSGIServer(('0.0.0.0', 5001), app).serve_forever()

最后,启动后愉快的玩耍吧

uwsgi --ini uwsgi.ini

(env1) root@root:~/xxx$ uwsgi --ini uwsgi.ini 
[uWSGI] getting INI configuration from uwsgi.ini
*** Starting uWSGI 2.0.18-debian (64bit) on [Fri Feb 26 17:10:39 2021] ***
compiled with version: 10.0.1 20200405 (experimental) [master revision 0be9efad938:fcb98e4978a:705510a708d3642c9c962beb663c476167e4e8a4] on 11 April 2020 11:15:55
os: Linux-5.4.0-62-generic #70-Ubuntu SMP Tue Jan 12 12:45:47 UTC 2021
nodename: xxxxx.com
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 4
current working directory: /home/root/xxx
detected binary path: /usr/bin/uwsgi-core
chdir() to /home/root/xxx
your processes number limit is 127484
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 TCP address 127.0.0.1:5001 fd 3
Python version: 3.8.5 (default, Jan 27 2021, 15:41:15)  [GCC 9.3.0]
Set PythonHome to /data/anaconda3
Python main interpreter initialized at 0x561843c3a060
python threads support enabled
your server socket listen backlog is limited to 4096 connections
your mercy for graceful operations on workers is 60 seconds
mapped 947960 bytes (925 KB) for 12 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x561843c3a060 pid: 3965990 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 3965990)
spawned uWSGI worker 1 (pid: 3965993, cores: 1)
spawned uWSGI worker 2 (pid: 3965994, cores: 1)
spawned uWSGI worker 3 (pid: 3965995, cores: 1)
spawned uWSGI worker 4 (pid: 3965996, cores: 1)
spawned uWSGI worker 5 (pid: 3965997, cores: 1)
spawned uWSGI worker 6 (pid: 3965998, cores: 1)
spawned uWSGI worker 7 (pid: 3965999, cores: 1)
spawned uWSGI worker 8 (pid: 3966000, cores: 1)
spawned uWSGI worker 9 (pid: 3966001, cores: 1)
spawned uWSGI worker 10 (pid: 3966002, cores: 1)
spawned uWSGI worker 11 (pid: 3966003, cores: 1)
spawned uWSGI worker 12 (pid: 3966004, cores: 1)

LoneKing