Apache2 mod_wsgi python 2.6 Django很慢

我已经尝试了大约1000倍的东西,但我似乎无法弄清楚为什么一个简单的Django网站使用apache 2.2.14 / wsgi latest / django 1.3很慢。 我已经证实了这个问题不是我们的数据库通过打开mysql慢查询日志logging。 我已经回顾了另一个100倍的wsgi守护进程configuration设置,但仍然不明白为什么runserver目前比apache更快!

这里是我的Apacheconfiguration,让我知道如果有其他项目将是有用的!

WSGIDaemonProcess xxx display-name=xxx group=www-data user=www-data processes=25 threads=1 <VirtualHost *:80> ServerName www.xxx.com ServerAlias xxx.com ServerAlias localhost ServerAdmin [email protected] RewriteEngine Off RewriteCond %{HTTP_HOST} ^xxx\.com$ [NC] RewriteRule ^(.*)$ http://www.xxx.com$1 [R=301,L] RewriteCond %{REQUEST_URI} ^/login/$ RewriteRule /login https://%{HTTP_HOST}%{REQUEST_URI} [R,L] RewriteCond %{REQUEST_URI} ^/signup/ RewriteRule /signup https://%{HTTP_HOST}%{REQUEST_URI} [R,L] ErrorLog /var/log/apache2/xxx-error.log LogLevel debug CustomLog /var/log/apache2/xxx-access.log combined WSGIProcessGroup %{GLOBAL} WSGIApplicationGroup %{GLOBAL} WSGIScriptAlias / /srv/xxx.com/mod_wsgi/dispatch.wsgi Alias /static /srv/xxx.com/src/xxx/static <Directory "/srv/xxx.com/src/xxx/static"> Order deny,allow Allow from all </Directory> </VirtualHost> 

自由:

  total used free shared buffers cached Mem: 496 489 6 0 1 17 -/+ buffers/cache: 471 25 Swap: 1023 50 973 

最佳:

 top - 21:30:52 up 2:06, 1 user, load average: 0.07, 0.10, 0.12 Tasks: 101 total, 2 running, 99 sleeping, 0 stopped, 0 zombie Cpu(s): 1.2%us, 1.2%sy, 0.0%ni, 95.4%id, 2.2%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 508272k total, 467788k used, 40484k free, 1448k buffers Swap: 1048572k total, 59576k used, 988996k free, 22708k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 5009 www-data 20 0 179m 41m 5024 R 20 8.3 0:02.59 apache2 2521 elastic 20 0 710m 70m 4052 S 1 14.2 0:54.32 java 5013 root 20 0 19272 1252 932 R 0 0.2 0:00.05 top 1 root 20 0 23628 1108 784 S 0 0.2 0:00.18 init 2 root 20 0 0 0 0 S 0 0.0 0:00.00 kthreadd 

这里是mpm_prefork_module设置:

 <IfModule mpm_prefork_module> StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxClients 150 MaxRequestsPerChild 0 </IfModule> 

你有:

 WSGIDaemonProcess xxx display-name=xxx group=www-data user=www-data processes=25 threads=1 

但是然后有:

 WSGIProcessGroup %{GLOBAL} 

这意味着您没有委派WSGI应用程序在该守护进程组中运行。

换句话说,您正在以embedded模式运行WSGI应用程序,而WSGIDaemonProcess指令是多余的。

如果您也在使用Apache prefork MPM,则可能由于Apache在其默认configuration中使用多达150个单线程进程而可能遇到速度问题。

因此,缓慢可能是由于您的WSGI应用程序的延迟加载,如果它很大,实际上有一个请求。

随着更多的请求进来,Apache必须不断推出新的stream程来满足日益增长的需求。 如果请求丢失,Apache将开始丢弃进程。 如果再次增加请求,则必须重新启动新的进程并重新加载应用程序。

现在这是一个极端的情况,你可能会受到多大的影响取决于Apache MPM设置如何设置,你没有显示,以及你的stream量configuration文件是什么样的。

在最糟糕的情况下,您甚至可能会覆盖MaxRequestsPerChild指令并告诉Apache在单个或less量请求后终止进程,因此您可能会一直强制重新加载应用程序。

有关这类问题的相关问题的一些阅读:

http://blog.dscpl.com.au/2009/03/load-spikes-and-excessive-memory-usage.html

所以这就是基于Apacheconfiguration的情况。

忽略守护进程模式的错误configuration,有可能成为应用程序问题。 为此,我会build议尝试一个性能监视工具。 我有偏见的build议是New Relic。 即使你不想支付这样的工具,它也会给你两周的所有function的试用,足以让你理清瓶颈在哪里。

有关New Relic可以为Python做什么的示例,请看:

http://blog.newrelic.com/2011/11/08/new-relic-supports-python/