在开发模式下,django 做了很多事情来简化开发(例如:代码重新加载;如果使用模板,则为每个请求呈现模板;...)。
在生产模式下,在首选 django 之前部署 WSGI 服务器。这样的wsgi可能是gunicorn, uWSGI, ...
一个典型的生产网络服务器的布局可能是:nginx -> gunicorn -> django
简单对比(django官方教程代码用一个非常简单的“Hello World!@time”模板):
{% block content %}
<p> Hello World! @ {{ now }}</p>
{% endblock %}
开发模式
直接用django运行
python manage.py runserver
运行 apache-ab 进行测试
ab -n1000 -c10 http://127.0.0.1:8000/polls/helloworld
Server Software: WSGIServer/0.2 # django
Server Hostname: 127.0.0.1
Server Port: 8000
Document Path: /polls/helloworld
Document Length: 59 bytes
Concurrency Level: 10
Time taken for tests: 3.438 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 242000 bytes
HTML transferred: 59000 bytes
Requests per second: 290.87 [#/sec] (mean)
Time per request: 34.380 [ms] (mean)
Time per request: 3.438 [ms] (mean, across all concurrent requests)
Transfer rate: 68.74 [Kbytes/sec] received
生产模式
用 gunicorn 运行
../bin/gunicorn -w4 mysite.wsgi # 有 4 个工人
运行 apache-ab 进行测试
ab -n1000 -c10 http://127.0.0.1:8000/polls/helloworld
Server Software: gunicorn/19.7.1 # gunicorn
Server Hostname: 127.0.0.1
Server Port: 8000
Document Path: /polls/helloworld
Document Length: 59 bytes
Concurrency Level: 10
Time taken for tests: 0.618 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 248000 bytes
HTML transferred: 59000 bytes
Requests per second: 1619.10 [#/sec] (mean)
Time per request: 6.176 [ms] (mean)
Time per request: 0.618 [ms] (mean, across all concurrent requests)
Transfer rate: 392.13 [Kbytes/sec] received