参考http://blog.csdn.net/u013378306/article/details/76215982

django 原生为单线程序,当第一个请求没有完成时,第二个请求辉阻塞,知道第一个请求完成,第二个请求才会执行。

可以使用uwsgi  编程多并发的

django 的并发能力真的是令人担忧,这里就使用 nginx + uwsgi 提供高并发

nginx 的并发能力超高,单台并发能力过万(这个也不是绝对),在纯静态的 web 服务中更是突出其优越的地方,由于其底层使用 epoll 异步IO模型进行处理,使其深受欢迎

做过运维的应该都知道,php 需要使用 nginx + fastcgi 提供高并发,java 需要使用 nginx + tomcat 提供 web 服务

下面介绍如何使用 nginx + uwsgi 为 django 提供高并发 web 服务

1、系统环境

[root@crazy-acong ~]# uname -a
Linux crazy-acong 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@crazy-acong ~]# cat /etc/redhat-release
CentOS release 6.6 (Final)

2、python 及 django 版本

[root@crazy-acong ~]# django-admin --version
1.10.6

3、安装 uwsgi 及 测试 uwsgi

3.1 安装

[root@crazy-acong ~]# pip install uwsgi

3.2 测试 uwsgi 提供 web 服务的功能

# 创建 test.py 文件
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2 # 启动 uwsgi 服务
[root@crazy-acong ~]# uwsgi --http :8000 --wsgi-file test.py # 查看启动进程
[root@crazy-acong ~]# netstat -lnpt | grep uwsgi
tcp 0 0 127.0.0.1:26685 0.0.0.0:* LISTEN 22120/uwsgi
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 22120/uwsgi # 在浏览器中访问 http://ip:8000 就可以看到 Hello World 字样了

3.3 将启动参数写入到配置文件中,然后进行启动 django 程序

3.3.1 创建 uwsgi 配置文件

[root@crazy-acong ~]# cd /data/django_test   # 进入到 django 的主目录

[root@crazy-acong django_test]# cat test-uwsgi.ini
[uwsgi]
# 对外提供 http 服务的端口
http = :9000 #the local unix socket file than commnuincate to Nginx 用于和 nginx 进行数据交互的端口
socket = 127.0.0.1:8001 # the base directory (full path) django 程序的主目录
chdir = /data/django_test # Django's wsgi file
wsgi-file = django_test/wsgi.py # maximum number of worker processes
processes = 4 #thread numbers startched in each worker process
threads = 2 #monitor uwsgi status 通过该端口可以监控 uwsgi 的负载情况
stats = 127.0.0.1:9191 # clear environment on exit
vacuum = true # 后台运行,并输出日志
daemonize = /var/log/uwsgi.log

3.3.2 通过 uwsgi 配置文件启动 django 程序

# 通过配置文件启动 django 程序
[root@crazy-acong django_test]# /usr/local/bin/uwsgi test-uwsgi.ini # 在浏览器中 通过访问 http://ip:9000 可以看到发布的 django 程序

 到这里就可以支持多并发了,不会阻塞了。如果要使用更好的性能,可以使用nginx

4、安装 nginx

nginx 安装参考 http://www.cnblogs.com/CongZhang/p/6548570.html

5、配置 nginx 的配置文件

在 django 的主目录下创建下面的 nginx 配置文件,然后做软连接到 nginx 的配置文件目录,或者直接在 nginx 配置文件目录中添加该文件也可以

5.1 创建 nginx 配置文件

[root@crazy-acong django_test]# cat /data/django_test/django-nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
} # configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8; # max upload size
client_max_body_size 75M; # adjust to taste # Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
} location /static {
alias /data/django_test/static; # your Django project's static files - amend as required
} # Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /data/django_test/uwsgi_params; # the uwsgi_params file you installed
}
}

5.2 重启nginx 服务

[root@crazy-acong django_test]# nginx -t
nginx: the configuration file /data/application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /data/application/nginx-1.10.3/conf/nginx.conf test is successful
[root@crazy-acong django_test]# nginx -s reload [root@crazy-acong django_test]# netstat -lnpt | grep 8000
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 43492/nginx

这个时候就可以通过 http://ip:8000 访问 django 程序了,不过目前还存在一个问题,访问 http://ip:8000/admin 发现静态文件貌似没读取到,需要通过下面的方法解决静态文件的问题

6、解决 django 多 app 静态文件的问题

# 在 django 程序的 settings.py 文件中添加以下内容

STATIC_ROOT = os.path.join(BASE_DIR, "static_all")

# 然后通过执行该命令,将静态文件整合到一个目录中
[root@crazy-acong django_test]# python3 manage.py collectstatic [root@crazy-acong django_test]# ll
total 40
drwxr-xr-x 3 nginx games 4096 Mar 14 14:42 app01
-rw-r--r-- 1 root root 3072 Mar 14 14:51 db.sqlite3
-rw-r--r-- 1 root root 1026 Mar 14 15:18 django-nginx.conf
drwxr-xr-x 3 nginx games 4096 Mar 14 15:45 django_test
-rwxr-xr-x 1 nginx games 809 Mar 14 14:37 manage.py
drwxr-xr-x 2 nginx games 4096 Mar 14 14:42 static
drwxr-xr-x 3 root root 4096 Mar 14 15:47 static_all # 此时会发现多了一个该目录,所有 app 的静态文件都整合到这一个目录中了
drwxr-xr-x 2 nginx games 4096 Mar 14 14:40 templates
-rw-r--r-- 1 root root 565 Mar 14 15:40 test-uwsgi.ini
-rw-r--r-- 1 root root 664 Mar 14 15:28 uwsgi_params

然后需要修改 nginx 配置文件中 指向 django 静态目录的配置文件

[root@crazy-acong django_test]# cat /data/django_test/django-nginx.conf
# the upstream component nginx needs to connect to
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
} # configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-8; # max upload size
client_max_body_size 75M; # adjust to taste # Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
} location /static {
     # 需要修改的地方在这里
alias /data/django_test/static_all; # your Django project's static files - amend as required
} # Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /data/django_test/uwsgi_params; # the uwsgi_params file you installed
}
}

最后重启 nginx 服务即可

[root@crazy-acong django_test]# nginx -t
nginx: the configuration file /data/application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /data/application/nginx-1.10.3/conf/nginx.conf test is successful
[root@crazy-acong django_test]# nginx -s reload

django 多并发,多线程。的更多相关文章

  1. Python框架下django 的并发和多线程

    django 的并发能力真的是令人担忧,django本身框架下只有一个线程在处理请求,任何一个请求阻塞,就会影响另一个情感求的响应,尤其是涉及到IO操作时,基于框架下开发的视图的响应并没有对应的开启多 ...

  2. java并发多线程显式锁Condition条件简介分析与监视器 多线程下篇(四)

    Lock接口提供了方法Condition newCondition();用于获取对应锁的条件,可以在这个条件对象上调用监视器方法 可以理解为,原本借助于synchronized关键字以及锁对象,配备了 ...

  3. java 并发多线程 锁的分类概念介绍 多线程下篇(二)

    接下来对锁的概念再次进行深入的介绍 之前反复的提到锁,通常的理解就是,锁---互斥---同步---阻塞 其实这是常用的独占锁(排它锁)的概念,也是一种简单粗暴的解决方案 抗战电影中,经常出现为了阻止日 ...

  4. java 并发多线程显式锁概念简介 什么是显式锁 多线程下篇(一)

    目前对于同步,仅仅介绍了一个关键字synchronized,可以用于保证线程同步的原子性.可见性.有序性 对于synchronized关键字,对于静态方法默认是以该类的class对象作为锁,对于实例方 ...

  5. 提高Django高并发性的部署方案(Python)

    方案: nginx + uWSGI 提高 Django的并发性        1. uWSGI :                 uWSGI是一个web服务器,实现了WSGI协议.uwsgi协议.h ...

  6. Java并发多线程 - 并发工具类JUC

    安全共享对象策略 1.线程限制 : 一个被线程限制的对象,由线程独占,并且只能被占有它的线程修改 2.共享只读 : 一个共享只读的对象,在没有额外同步的情况下,可以被多个线程并发访问, 但是任何线程都 ...

  7. Java基础】并发 - 多线程

    Java基础]并发 - 多线程 分类: Java2014-05-03 23:56 275人阅读 评论(0) 收藏 举报 Java   目录(?)[+]   介绍 Java多线程 多线程任务执行 大多数 ...

  8. java concurrent 并发多线程

    Concurrent 包结构 ■ Concurrent 包整体类图 ■ Concurrent包实现机制 综述: 在整个并发包设计上,Doug Lea大师采用了3.1 Concurrent包整体架构的三 ...

  9. (坑)django test在多线程下的问题

    问题描述: 使用django自带的test做测试,尝试去数据库中取数据,主线程中没有问题,非主线程中取不到数据. 示例代码: class MyTestCase(TestCase): def setUp ...

随机推荐

  1. socket failed:EACCES(Permission denied)

    1. 权限问题 安卓端写的TCP协议软件报错原因是建立的套接字没有限权对外连接. 在AndroidManifest.xml中,加上这一句话,取得权限. <uses-permission andr ...

  2. JAVA工具类 UUID

    UUID:通用惟一识别:Universally Unique Identifier: 在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的. GUID是一个128位长的数字,一般用16进制 ...

  3. hdoj 1288 Hat's Tea

    Hat's Tea Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  4. 利用jquery的contains实现搜索功能

    / jquery实现的搜索功能 $('#search_btn').on('click',function(){ var txt=$('#inputValue').val(); var value=$( ...

  5. ext,exrReturn新增,修改删除等用

    package cn.edu.hbcf.common.vo; /** * Ext Ajax 返回对象 * * @author * @date 2012-02-21 19:30:00 * */ publ ...

  6. TCP/IP笔记(八)应用层协议

    TCP/IP的应用层涵盖了OSI参考模型中第5.第6.第7层的所有功能,不仅包含了管理通信连接的会话层功能.转换数据格式的标识层功能,还包括与对端主机交互的应用层功能在内的所有功能. 利用网络的应用程 ...

  7. 【PM面试题】如果让你创业,你会选择什么?

    答案及理由 我会选择可穿戴设备 ,理由有三: 互联网与硬件的结合是未来的大势所趋,通过硬件来采集数据,而通过互联网或者移动互联网将这些设备连接起来,交换数据,让其形成流动的信息. 未来会从卖产品的阶段 ...

  8. 蓝桥杯 第三届C/C++预赛真题(1) 微生物增值(数学题)

    假设有两种微生物 X 和 Y X出生后每隔3分钟分裂一次(数目加倍),Y出生后每隔2分钟分裂一次(数目加倍). 一个新出生的X,半分钟之后吃掉1个Y,并且,从此开始,每隔1分钟吃1个Y. 现在已知有新 ...

  9. Java中带包的类的编译与执行

    http://blog.csdn.net/wbrs13/article/details/4859880

  10. springboot如何直接读取webapp下页面?

    公司改用springboot的时候,将页面相关的文件都放在了src/main/webapp下,我直接通过main方式启动的时候,无法读取到src/mian/webapp下文件,但是通过spring-b ...