Django / Apache / mod_wsgi网站上的CPU高负载


10

在具有Apache Bench的AWS小型实例(Ubuntu 10.04)上对django 1.21 / Apache / mod_wsgi配置进行负载测试时,在并发请求较少的情况下,CPU负载(使用正常运行时间和vmstat)显示出极高的CPU负载:

ab -c 5 -n 1000 "my_url"

...导致正常运行时间输出:

18:04:54 up 9 days, 16:54,  3 users,  load average: 5.33, 2.45, 1.91

即使Apache Bench并发值为2,CPU的占用率也为100%。关于什么问题的想法,或者我应该如何继续调试呢?

细节:

  • 绝望的是,我安装了带有简单“ Hello World”视图(没有DB调用等)的django项目/应用程序。结果相同。所以我怀疑这是我的应用程序代码。
  • 在负载测试期间,内存使用情况看起来不错。

这是负载测试之前/期间/之后的vmstat输出:

procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
0  0      0 1034484  94848 321320    0    0     0     0   13   29  0  0 100  0
6  0      0 1032916  94848 321328    0    0     0     0  262  720  4 32 12  0
6  0      0 1031684  94848 321336    0    0     0     0  312  796  7 33  0  0
8  0      0 1030892  94856 321344    0    0     0    12  302  763  4 36  0  0
...
6  0      0 1030268  94864 321376    0    0     0     0  302  843  3 39  0  0
0  0      0 1032452  94868 321380    0    0     0    12  183  516  3 22 34  0
1  0      0 1033988  94868 321388    0    0     0     0   24   38  1  2 92  0
0  0      0 1033996  94868 321388    0    0     0     0   17   28  0  0 100  0
  • 我正在运行apache2的prefork版本,因为我还运行了依赖PHP的WordPress。(PHP在Apache worker版本中不能很好地发挥作用)

这是我的虚拟主机文件:

WSGIPythonHome /home/xxx/webapps/ve/api
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerName  app.xxx.mobi

        WSGIDaemonProcess snaplive user=www-data group=www-data processes=10 threads=1 maximum-requests=10000
        WSGIProcessGroup snaplive
        WSGIScriptAlias / /home/xxx/webapps/api/settings/apache/prod.wsgi
        DocumentRoot /home/xxx/webapps/api/static
        ErrorLog /var/log/apache2/django-live/error.log
        CustomLog /var/log/apache2/django-live/access.log combined
</VirtualHost>

这是我的httpd.conf文件:

Alias /media /home/xxx/Django-1.2.1/django/contrib/admin/media
LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so

StartServers 2
MinSpareServers 2
MaxSpareServers 5
MaxClients 50
MaxRequestsPerChild 3000
ServerLimit 8
Keepalive off
HostnameLookups Off

这是我的wsgi文件:

import os
import sys
sys.stdout = sys.stderr

from django.core.handlers.wsgi import WSGIHandler
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
application = WSGIHandler()

sys.path.append("/home/xxx/webapps/api")

通过在负载测试期间从浏览器访问Django URL,我已经定性地确认了高CPU负载正在影响性能。

我读到这可能并不重要,但是我在错误日志中看到了很多:

[Sun Sep 19 18:04:58 2010] [error] Exception KeyError: KeyError(-1218693376,) in <module 'threading' from '/usr/lib/python2.6/threading.pyc'> ignored

如果有帮助,这是我的Apache基准测试结果:

Server Software:        Apache/2.2.14
Server Hostname:        app.xxx.mobi
Server Port:            80

Document Path:          /plist_catalog/test_data
Document Length:        0 bytes

Concurrency Level:      5
Time taken for tests:   27.720 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Non-2xx responses:      1000
Total transferred:      269000 bytes
HTML transferred:       0 bytes
Requests per second:    36.08 [#/sec] (mean)
Time per request:       138.598 [ms] (mean)
Time per request:       27.720 [ms] (mean, across all concurrent requests)
Transfer rate:          9.48 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        1    2   8.5      1      88
Processing:     9  136 176.9     81    1182
Waiting:        9  135 176.6     81    1182
Total:         10  138 176.7     83    1183

Percentage of the requests served within a certain time (ms)
  50%     83
  66%     98
  75%    128
  80%    140
  90%    423
  95%    576
  98%    727
  99%    819
 100%   1183 (longest request)

Answers:


2

问题是我安装了软件包apache2-mpm-itk而不是apache2-mpm-prefork。apache2-mpm-itk源自apache2-mpm-prefork,但由于某些原因,与mod_wsgi一起使用时效果不佳。


当使用ITK MPM和mod_wsgi时,建议在嵌入式进程中使用mod_wsgi守护程序模式并禁用Python解释器用法。如果使用嵌入式模式,那么您将像CGI一样在每个请求上加载应用程序。
Graham Dumpleton 2011年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.