使用Flask-SocketIO + gevent + uWSGI + nginx生成多个uWSGI Worker

问题描述

我有一个在Ubuntu上运行带有uWsgi,gevent和Nginx的Flask应用程序,但是从Flask-SocketIO docs中看不到如何运行多个uWsgi worker。文档建议您必须使用单个worker(+ thread)运行多个uWsgi实例,但是我不知道它的配置方式。

我在socketio_nodes下(根据文档)在Nginx配置的不同端口上添加了4个服务器,并且一旦指向uwsgi.ini,我就启动uwsgi。我如何最终运行4个单独的进程?

应用摘要

from gevent import monkey
monkey.patch_all()
.
.
.
if __name__=='__main__':
    from db import db
    db.init_app(app)
    socketio.run(app,host=config.HOST,port=config.PORT,debug=config.IS_DEBUG)

配置代码段:

HOST = '127.0.0.1'
PORT = 5000

uwsgi.ini

[uwsgi]
gevent = 1000

http = :5000

strict = true          
master = true
enable-threads = true  
vacuum = true              
single-interpreter = true  
need-app = true                              
die-on-term = true                             
http-websockets = true

processes = 1
threads = 1
harakiri = 60                                 

wsgi-file = app.py
callable = app

logto = /var/www/html/project/log/%n.log

Nginx配置代码段:

upstream socketio_nodes {
        ip_hash;

        server 127.0.0.1:5000;
        server 127.0.0.1:5001;
        server 127.0.0.1:5002;
        server 127.0.0.1:5003;
}

server {
        listen 443 ssl;
        listen [::]:443 ssl;
        real_ip_header X-Forwarded-For;
        set_real_ip_from 127.0.0.1;
        set_real_ip_from 192.168.1.1;
        real_ip_recursive on;

        root /var/www/html/project;
        index index.PHP index.html index.htm index.Nginx-debian.html;
        server_name [myurl] www.[myurl];

        location / {
                include proxy_params;
                proxy_http_version 1.1;
                proxy_pass http://127.0.0.1:5000;
                include uwsgi_params;
        }

        location /socket.io {
                include proxy_params;
                proxy_http_version 1.1;
                proxy_buffering off;
                proxy_set_header Upgrade $http_upgrade;         
                proxy_set_header Connection "Upgrade";                
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://socketio_nodes/socket.io;
                include uwsgi_params;
        }

        location /static {
                alias /var/www/html/project/static;
                expires 30d;
        }

.
.
.

输出

*** Starting uWsgi 2.0.19.1 (64bit) on [Thu Sep 10 12:42:24 2020] ***
compiled with version: 7.5.0 on 17 June 2020 16:34:28
os: Linux-4.15.0-74-generic #84-Ubuntu SMP Thu Dec 19 08:06:28 UTC 2019
nodename: project
machine: x86_64
clock source: unix
pcre jit disabled
detected number of cpu cores: 2
current working directory: /var/www/html/project
detected binary path: /var/www/html/project/venv2/bin/uwsgi
your processes number limit is 15532
your memory page size is 4096 bytes
 *** WARNING: you have enabled harakiri without post buffering. Slow upload Could be rejected on post-unbuffered webservers *** 
detected max file descriptor number: 1024
- async cores set to 1000 - fd table size: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWsgi http bound on :5000 fd 5
uwsgi socket 0 bound to TCP address 127.0.0.1:39459 (port auto-assigned) fd 4
Python version: 3.6.9 (default,Jul 17 2020,12:50:27)  [GCC 8.4.0]
Python main interpreter initialized at 0x5641e7a01040
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 21036928 bytes (20543 KB) for 1000 cores
*** Operational MODE: async ***
Wsgi app 0 (mountpoint='') ready in 2 seconds on interpreter 0x5641e7a01040 pid: 31753 (default app)
spawned uWsgi master process (pid: 31753)
spawned uWsgi worker 1 (pid: 31785,cores: 1000)
spawned uWsgi http 1 (pid: 31786)
*** running gevent loop engine [addr:0x5641e7379a60] ***

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)