【问题标题】:Performance of using Tornado and Nginx is similar to using Tornado only使用 Tornado 和 Nginx 的性能类似于仅使用 Tornado
【发布时间】:2016-03-16 09:49:05
【问题描述】:

我尝试通过使用 Tornado(4 个进程)、Supervisor 和 Nginx 来提高我的服务性能。配置设置后,我使用 Siege 测试服务。

但是,结果并没有显示出使用多个流程的任何竞争力。我认为这种组合可以处理比单个进程更多的请求,但事实并非如此。我仍然无法弄清楚原因,因为 Nginx 似乎成功地将请求分派到不同的 Tornado 进程。

supervisord.conf:

[group:a]
programs=a-server, a-db

[program:a-server]
command=python2.7 /my/path/to/app/server.py --port=80%(process_num)02d
directory=/my/path/to/app
numprocs=4
process_name=%(program_name)s%(process_num)d
autorestart=true
loglevel=info
redirect_stderr=true
stdout_logfile=/my/path/to/app/server.log
stdout_logfile_maxbytes=50MB
stdout_logfile_backups=10

[program:a-db]
command=mongod --dbpath /path/to/db
numprocs=1
process_name=db
autorestart=true
redirect_stderr=true
stdout_logfile=/my/path/to/app/db.log
stdout_logfile_maxbytes=30MB
stdout_logfile_backups=10

[inet_http_server]
port=127.0.0.1:9001

[supervisord]
logfile=/my/path/to/app/supervisord.log
logfile_maxbytes=50MB
logfile_backups=10
pidfile=/my/path/to/app/supervisord.pid


[supervisorctl]
serverurl=http://127.0.0.1:9001


[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

Nginx.conf:

worker_processes 1;

error_log /my/path/to/app/nginx.log;
pid /my/path/to/app/nginx.pid;

events {
    worker_connections  1024;
    # Using Mac
    use kqueue;
}

http {
    upstream a {
        server 127.0.0.1:8000;
        server 127.0.0.1:8001;
        server 127.0.0.1:8002;
        server 127.0.0.1:8003;
    }

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    access_log /my/path/to/app/access.log;

    gzip on;
    gzip_min_length 20;
    gzip_proxied any;
    gzip_types text/plain text/css text/xml application/javascript
               application/x-javascript application/xml
               application/atom+xml text/javascript;

    server {
        listen 80;
        server_name localhost;

        location /nginx_status {
             stub_status on;
             access_log off;
             allow 127.0.0.1;
             deny all;
        }

        location / {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://a;
        }
    }
}

请帮我找出问题所在,或者请告诉我如何剖析并找到瓶颈!非常感谢!

ENV:mac book pro,CPU:2.4 GHz Intel Core 2 Duo。

【问题讨论】:

    标签: python nginx tornado supervisord


    【解决方案1】:

    一些提示:

    # calculated sometimes as 2 * Number of CPUs, sometimes like number of backends + spare e.g.
    # 1 is not enough, is like a Tornado itself
    worker_processes  4;
    
    events {
        worker_connections  19000;  # It's the key to high performance - have a lot of connections available
    }
    
    worker_rlimit_nofile    20000;  # Each connection needs a filehandle (or 2 if you are proxying)
    
    tcp_nopush on;
    tcp_nodelay on;
    sendfile on;
    
    # allow the server to close the connection after a client stops responding. 
    reset_timedout_connection on;
    
    # Accept as many connections as possible, after nginx gets notification about a new connection.
    # May flood worker_connections, if that option is set too low.
    multi_accept on;
    

    您可以服务的用户总数 = worker_processes * worker_connections。

    还要检查一些 nginx 进程限制,例如 nofile、ulimit。

    还可以查看Tuning nginx worker_process to obtain 100k hits per min(我复制了更大的部分)

    【讨论】:

      猜你喜欢
      • 2015-02-25
      • 2013-04-24
      • 2015-12-28
      • 2020-10-17
      • 1970-01-01
      • 2011-12-29
      • 2013-10-30
      • 2015-03-06
      • 2023-03-04
      相关资源
      最近更新 更多