运行的uwsgi线程太多了

时间:2016-01-29 10:38:15

标签: python django multithreading nginx uwsgi

早上好,

我是网络服务器基础架构和管理的新手。我尝试使用nginx + uwsgi + django和python的web服务器存在以下问题:每次刷新或请求用户正在进行的网页时,uwsgi创建2-3个永不终止的新线程。几天后,我最终有超过30000个线程,我必须重新加载uwsgi以保持网页的性能。

要检查线程数,请使用以下命令: ps - eLf | grep uwsgi(您可以看到附带的结果)。

我的uwsgi配置如下:

[uwsgi]

vhost = true

socket = /tmp/mySocket.sock

master = true

processes = 4

max_request =  300

vacuum = true

die-on-term = true

close-on-exec = true

harakiri = 30

wsgi-file = /home/virtualEnv/server/wsgi.py

virtualenv = /home/virtualEnv

pythonpath = /home/virtualEnv/myServer

chdir=/home/virtualEnv/myServer

pidfile=/tmp/myFile.pid

daemonize = /var/log/uwsgi/uwsgi-@(exec://date +%%Y-%%m-%%d).log

log-reopen = true

chmod-socket = 664

gid = www-data

uid = www-data

我的uwsgi.py文件如下:

import os
import sys

path = ‘/home/virtualEnv/myServer'
if path not in sys.path:
   sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'myServer.settings'

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

我的/etc/init/uwsgi.conf文件是:

description "uWSGI Emperor"
start on runlevel [2345]
stop on runlevel [!2345]

respawn
exec uwsgi --emperor /etc/uwsgi/vassals/ --wsgi-file /home/virtualEnv/server/wsgi.py

我尝试过使用带有和不带线程的uwsgi,有没有--thunder-lock但没有真正改变。

编辑:

清理uwsgi.ini文件后,我继续遇到同样的问题。文件的当前配置是:

uwsgi.ini:

[uwsgi]
socket = /tmp/mySocket.sock
master = true
processes = 4
max_request =  3
vacuum = true
die-on-term = true
close-on-exec = true
harakiri = 30
wsgi-file = /home/virtualEnv/server/wsgi.py
virtualenv = /home/virtualEnv
pythonpath = /home/virtualEnv/myServer
chdir=/home/virtualEnv/myServer
pidfile=/tmp/myFile.pid
logger = file:/var/log/uwsgi/uwsgi-@(exec://date +%%Y-%%m-%%d).log
log-reopen = true
chmod-socket = 664
gid = www-data
uid = www-data

uwsgi.conf

description "uWSGI Emperor"
start on runlevel [2345]
stop on runlevel [!2345]

respawn
exec uwsgi --emperor /etc/uwsgi/vassals/

nginx.conf:

user www-data;
worker_processes auto;
worker_rlimit_nofile 10000;
pid /run/nginx.pid;
events {
worker_connections 10000;
multi_accept on;
use epoll;
}

http {
server_tokens off;
resolver 8.8.8.8;
map $http_accept_language $lang {
default en;
~*en en;
~*pt pt;
~*fr fr;
~*it it;
~*es es;
~*ru ru;
~*ro ro;}
client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 10m;
large_client_header_buffers 2 1k;
client_body_timeout 12;
client_header_timeout 12;
keepalive_requests 100;
send_timeout 10;
open_file_cache max=2500 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
sendfile off;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 5000;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
charset UTF-8;
gzip on;
gzip_http_version 1.0;
gzip_vary on;
gzip_static on;
gzip_disable ""msie6"";
gzip_min_length 256;
gzip_comp_level 1;
gzip_buffers 4 32k;
gzip_proxied any;
gzip_types text/plain text/html text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}"

1 个答案:

答案 0 :(得分:0)

您正在启动uWSGI皇帝,所以您不应该直接将wsgi文件传递给它。从uwsgi.conf文件中删除该参数。

使用emperor时,你不应该对你的uWSGI实例进行守护,因为emperor会断开与它的连接,这可能会导致每个请求产生新的实例(皇帝不知道有一些实例,因为他们已经守护进程,因此它产生了新的处理请求)。您应该从uWSGI配置中删除daemonize

此外,在明确指向wsgi文件时使用vhost不应该完成。如果您不知道此参数的作用或认为您不需要它,请将其删除。

更改后,您的文件应如下所示:

uwsgi.ini文件:

[uwsgi]


socket = /tmp/mySocket.sock
master = true
processes = 4
max_request =  300
vacuum = true
die-on-term = true
close-on-exec = true
harakiri = 30
wsgi-file = /home/virtualEnv/server/wsgi.py
virtualenv = /home/virtualEnv
pythonpath = /home/virtualEnv/myServer
chdir=/home/virtualEnv/myServer
pidfile=/tmp/myFile.pid
log-reopen = true
chmod-socket = 664

gid = www-data
uid = www-data

uwsgi.conf文件:

description "uWSGI Emperor"
start on runlevel [2345]
stop on runlevel [!2345]

respawn
exec uwsgi --emperor /etc/uwsgi/vassals/

wsgi.py文件无法更改。

如果您想从uwsgi服务器获取日志文件,请使用:

logger                  = file:/var/log/uwsgi/uwsgi-@(exec://date +%%Y-%%m-%%d).log

而不是daemonize