1. 程式人生 > >django 多併發,多執行緒。

django 多併發,多執行緒。

django 原生為單執行緒序,當第一個請求沒有完成時,第二個請求輝阻塞,知道第一個請求完成,第二個請求才會執行。

可以使用uwsgi  程式設計多併發的

django 的併發能力真的是令人擔憂,這裡就使用 nginx + uwsgi 提供高併發

nginx 的併發能力超高,單臺併發能力過萬(這個也不是絕對),在純靜態的 web 服務中更是突出其優越的地方,由於其底層使用 epoll 非同步IO模型進行處理,使其深受歡迎

做過運維的應該都知道,php 需要使用 nginx + fastcgi 提供高併發,java 需要使用 nginx + tomcat 提供 web 服務

下面介紹如何使用 nginx + uwsgi 為 django 提供高併發 web 服務

1、系統環境

[[email protected] ~]# 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
[[email protected]-acong ~]# cat /etc/redhat-release 
CentOS release 6.6 (Final)

2、python 及 django 版本

[[email protected]-acong ~]# django-admin --version
1.10.6

3、安裝 uwsgi 及 測試 uwsgi

3.1 安裝

[[email protected] ~]# 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 服務
[
[email protected]
-acong ~]# uwsgi --http :8000 --wsgi-file test.py # 檢視啟動程序 [[email protected]-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 配置檔案

複製程式碼
[[email protected] ~]# cd /data/django_test   # 進入到 django 的主目錄

[[email protected]-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 程式
[[email protected]-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 配置檔案

複製程式碼
[[email protected] 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 服務

複製程式碼
[[email protected] 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
[[email protected]-acong django_test]# nginx -s reload
      
[[email protected]-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")


# 然後通過執行該命令,將靜態檔案整合到一個目錄中
[[email protected]-acong django_test]# python3 manage.py collectstatic

[[email protected]-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 靜態目錄的配置檔案

複製程式碼
[[email protected] 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 服務即可

[[email protected] 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
[[email protected]-acong django_test]# nginx -s reload