加速 Django 服务器
我的设置是 django 1.3 以及 ubuntu 10.04 的默认 mod_wsgi 和 apache 包。我在我的开发虚拟机上测试了我的应用程序的一个视图(调试和调试工具栏关闭):
ab -n 200 -c 5 http://127.0.0.1/
每秒收到 4 个请求。这看起来很慢,所以我简化了查询、使用索引等,调试工具栏告诉我有 4 个查询需要 8 毫秒。运行相同的测试,我每秒仅收到 8 个请求。 CPU 似乎一直处于 100%。对于现在非常简单的视图来说,这似乎相当慢,但它只是一个低功率的虚拟机。
我决定启动一个大型 ec2 实例(4 个 cpu)来看看在该类机器上能获得什么样的性能,并且惊讶地发现每秒只收到 13 个请求。如何更改 apache/mod_wsgi 的配置以获得此类机器的更多性能?
我想我正在使用worker而不是prefork:
$ /usr/sbin/apache2 -l
Compiled in modules:
core.c
mod_log_config.c
mod_logio.c
worker.c
http_core.c
mod_so.c
我的worker配置是:
<IfModule mpm_worker_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
我的WSGI设置如下:
WSGIScriptAlias / /home/blah/site/proj/wsgi.py
WSGIDaemonProcess blah user=blah group=blah processes=1 threads=10
WSGIProcessGroup blah
非常感谢您的帮助!
注意:我尝试了另一个实例的 ab 测试并得到了相同的结果
My setup is django 1.3 and the default mod_wsgi and apache packages for ubuntu 10.04. I tested one view of my app on my development VM (DEBUG and debugging toolbar off):
ab -n 200 -c 5 http://127.0.0.1/
and got 4 requests per second. This seemed slow so I simplified the queries, used indexes, etc. to the point where debugging toolbar tells me I have 4 queries taking 8ms. Running the same test, I only get 8 requests per second. The CPU seems to be at 100% the whole time. This seems quite slow for what is now a quite simple view, but it is just a low powered VM.
I decided to start up a large ec2 instance (4 cpu) to see what kind of performance I would get on that class of machine and was surprised to only get 13 requests per second. How can I change the configuration of apache/mod_wsgi to get more performance out of this class of machine?
I think I am using worker rather than prefork:
$ /usr/sbin/apache2 -l
Compiled in modules:
core.c
mod_log_config.c
mod_logio.c
worker.c
http_core.c
mod_so.c
My worker configuration is:
<IfModule mpm_worker_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
and my WSGI settings look like this:
WSGIScriptAlias / /home/blah/site/proj/wsgi.py
WSGIDaemonProcess blah user=blah group=blah processes=1 threads=10
WSGIProcessGroup blah
Thanks very much for your help!
NOTE: I tried the ab test from another instance and got the same result
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保保持活动状态已关闭。
更多进程和单线程我看到了更好的性能,其中 CPU 是限制因素;尝试
进程=4线程=1
。Make sure keep-alive is off.
More processes and single-threaded I have seen better performance where CPU is the limiting factor; try
processes=4 threads=1
.调整 mod_wsgi 的最好方法是不使用它:)
首先:我不认为你的问题是网络服务器:使用 mod_wsgi 你可以每秒收到数百个请求。您可以通过缓存和数据库连接池获得更好的结果。如果您使用的是 postgres,请查看 pgpool II:http://pgpool.projects.postgresql.org/ 。
但是,如果您想深入了解 wsgi Web 服务器,请仔细阅读这个不错的基准测试:http: //nichol.as/benchmark-of-python-web-servers 。
如果你不需要异步worker,gunicorn是一个不错的选择。它的设置非常简单(您可以使用 manage.py run_gunicorn 运行它)并且速度非常快。如果您想确定 mod_wsgi 不是 cuprit,请尝试一下。如果你想要更好的性能,请使用 gevent 或 uWSGI。
但 Web 服务器不会改变您的基准:您可以从 4 req/s 变为 4.01 req/s。
The best way to tweak mod_wsgi is to not use it :)
First: I don't think your problem is the web server: with mod_wsgi you can get hundreds requests/s. You can get better results with caching and with DB connection pooling. If you're using postgres, take a look at pgpool II: http://pgpool.projects.postgresql.org/ .
However, if you want to go deeper into wsgi web servers, read carefully this nice benchmark: http://nichol.as/benchmark-of-python-web-servers .
If you don't need asyncronous workers, gunicorn is a good choice. It's very easy to setup (you can run it with manage.py run_gunicorn) and it's pretty fast. If you want to be sure that mod_wsgi is not the cuprit, try it. If you want better performance go with gevent or uWSGI.
But the Web Server won't change your benchmark: you can go from 4 req/s to 4.01 req/s.