参考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 socketserver 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 onlisten      8000;# the domain name it will serve forserver_name .example.com; # substitute your machine's IP address or FQDNcharset     utf-8;# max upload sizeclient_max_body_size 75M;   # adjust to taste# Django medialocation /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 socketserver 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 onlisten      8000;# the domain name it will serve forserver_name .example.com; # substitute your machine's IP address or FQDNcharset     utf-8;# max upload sizeclient_max_body_size 75M;   # adjust to taste# Django medialocation /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

转载于:https://www.cnblogs.com/1204guo/p/8056641.html

django 多并发,多线程。相关推荐

  1. python diango 并发_利用gunicorn提高django的并发能力

    引言 手头上的项目有一些采用django框架编写, 如果说并发量比较小的时候简单的runserver是可以应对的. 那么当并发达到一两千的时候,该怎么提高django的并发能力呢? Overview ...

  2. Java 并发/多线程教程(四)-并发模型

    本系列译自jakob jenkov的Java并发多线程教程(本章节部分内容参考http://ifeve.com/并发编程模型),个人觉得很有收获.由于个人水平有限,不对之处还望矫正! 并发系统可以有多 ...

  3. Java 并发/多线程教程(五)-相同线程

    本系列译自jakob jenkov的Java并发多线程教程,个人觉得很有收获.由于个人水平有限,不对之处还望矫正! 相同线程是一并发框架模型,是一个单线程系统向外扩展成多个单线程的系统.这样的结果就是 ...

  4. Java并发/多线程教程——1

    本系列译自jakob jenkov的Java并发多线程教程,个人觉得很有收获.由于个人水平有限,不对之处还望矫正!在早期,计算机只有一个CPU,同一时刻只能执行一个程序,后来有了多任务的说法,多任务是 ...

  5. java并发多线程面试_Java多线程并发面试问答

    java并发多线程面试 Today we will go through Java Multithreading Interview Questions and Answers. We will al ...

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

    java 并发多线程显式锁概念简介 什么是显式锁 多线程下篇(一) 目前对于同步,仅仅介绍了一个关键字synchronized,可以用于保证线程同步的原子性.可见性.有序性 对于synchronize ...

  7. 推测的删除锁(Speculative Lock Elision):实现高并发多线程执行

    背景 SLE全称Speculative Lock Elision,我称之为推测的删除锁.这是一篇关于SLE的论文翻译,但是因为本人英语功底很差,所以翻译的不通顺而且会有很多错误的地方.之所以把它发出来 ...

  8. JUC并发多线程进阶

    笔记整理来源 B站UP主狂神说Java https://space.bilibili.com/95256449/ JUC并发多线程进阶 1.什么是JUC 源码+官方文档 JUC是 java util ...

  9. 高并发多线程分片断点下载

    基于Java的高并发多线程分片断点下载 首先直接看测试情况: 单线程下载72MB文件 7线程并发分片下载72MB文件: 下载效率提高2-3倍,当然以上测试结果还和设备CPU核心数.网络带宽息息相关. ...

最新文章

  1. 哈佛CASTER | 基于化学子结构表征预测药物相互作用
  2. IAP的原理和stm8的IAP
  3. java学习笔记—国际化(41)
  4. 莫队(不带修改)模板
  5. Android 曝光采集:以商品 view 曝光量的统计为例
  6. erlang rebar 配置mysql_Erlang Rebar 使用指南之四:依赖管理
  7. C#绘图工具之Move
  8. javascript操作表格案例讲解
  9. python代码编辑教程_python教程:pycharm编写代码的方式教学
  10. modal 弹出层后禁止底层滚动
  11. java实现手机开关机_Android自动开关机实现
  12. x509证书,SSL详解
  13. 矩阵转置+矩阵相加(三元组)
  14. MATLAB中imcrop函数
  15. 苹果小企业项目申请App Store Small Business Program
  16. 汽车冬季养护的四个重点
  17. ibm vios_使用IBM地理分散弹性解决方案在生产现场保留冗余VIOS配置
  18. 接口测试是什么?为什么要做接口测试?
  19. nginx端口映射配置(Windows)
  20. java scanner输入数组_Java Scanner输入两个数组的方法

热门文章

  1. oracle存储过程如何传递一个bean对象_java程序员如何在短期内抓住面试重点,成为大厂offer收割机...
  2. tcpdump工具编译记录
  3. 【kafka】kafka 消息头的强大功能
  4. 【Fllink】Connection to ZooKeeper suspended. Can no longer retrieve the leader from ZooKeeper
  5. 【Flink】Flink 1.12.2 启动脚本
  6. 【Elasticsearch】Resizing Elasticsearch shards for fun and profit
  7. 【Elasticsearch】es 7.8.0 java 实现 BulkRequest 批量写入数据
  8. 【Elasticsearch】Elasticsearch analyzer 中文 分词器
  9. 【Java】finally 语句块不被执行的几种情况
  10. 【Java】String hashCode 这个数字 31