Django中配置Gunicorn

Gunicorn是一個被廣泛使用的高性能的Python WSGI UNIX HTTP服務(wù)器,移植至Ruby的獨角獸(Unicorn)項目,使用pre-fork worker模式,具有使用非常簡單,輕量級的資源消耗,以及高性能等特點。

Django自帶的簡易服務(wù)器,它是一個純Python寫的輕量級的WEB服務(wù)器,但它是為了開發(fā)而設(shè)計的,不適合在生產(chǎn)環(huán)境中部署。

python manage.py runserver 0.0.0.0:8000

在Django中配置gunicorn,適合高并發(fā)的生產(chǎn)環(huán)境。

特點

  • Gunicorn是基于prefork模式的Python wsgi應(yīng)用服務(wù)器,支持 Unix like的系統(tǒng)

  • 采用epoll (Linux下) 非阻塞網(wǎng)絡(luò)I/O 模型

  • 多種Worker類型可以選擇 同步的,基于事件的(gevent tornado等),基于多線程的

  • 高性能,比之uwsgi不相上下

  • 配置使用非常簡單

  • 支持 Python 2.x >= 2.6 or Python 3.x >= 3.2

安裝

pip install gunicorn

配置

  • settings.py

在settings.py添加gunicorn。

INSTALLED_APPS = [
    ...
    'gunicorn',
    ...
]
  • gunicorn配置文件gunicorn.conf.py
import logging
import logging.handlers
from logging.handlers import WatchedFileHandler
import os
import multiprocessing

bind = "0.0.0.0:8091"   #綁定的ip與端口
backlog = 512                #監(jiān)聽隊列數(shù)量,64-2048
#chdir = '/home/test/server/bin'  #gunicorn要切換到的目的工作目錄
worker_class = 'sync' #使用gevent模式,還可以使用sync 模式,默認的是sync模式
workers = 4 # multiprocessing.cpu_count()    #進程數(shù)
threads = 16 #multiprocessing.cpu_count()*4 #指定每個進程開啟的線程數(shù)
loglevel = 'info' #日志級別,這個日志級別指的是錯誤日志的級別,而訪問日志的級別無法設(shè)置
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'

# accesslog = "/home/log/gunicorn_access.log"      #訪問日志文件
#errorlog = "/home/log/gunicorn_error.log"        #錯誤日志文件
accesslog = "-"  #訪問日志文件,"-" 表示標準輸出
errorlog = "-"   #錯誤日志文件,"-" 表示標準輸出

proc_name = 'fof_api'   #進程名

其中access_log_format選項的變量含義如下:

其每個選項的含義如下:
h          remote address
l          '-'
u          currently '-', may be user name in future releases
t          date of the request
r          status line (e.g. ``GET / HTTP/1.1``)
s          status
b          response length or '-'
f          referer
a          user agent
T          request time in seconds
D          request time in microseconds
L          request time in decimal seconds
p          process ID

accesslog、errorlog日志文件可以寫到文件,也可以標準輸出到屏幕。

正常啟動

以gunicorn.conf.py配置文件啟動gunicorn

gunicorn -c gunicorn.conf.py api.wsgi:application

也可以直接啟動

gunicorn api.wsgi:application -b 0.0.0.0:8091 -w 4 -k gthread

-w 4是啟動4個進程,可以支持更多的并發(fā)。

or

gunicorn api.wsgi:application -b 0.0.0.0:8091 -w 4 -k gthread  --thread 40 --max-requests 4096 --max-requests-jitter 512

參數(shù)詳見官網(wǎng)的配置說明,參考文章底部的鏈接

Docker方式啟動

Dockerfile文件

FROM python:3.5
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
 
COPY requirements.txt /usr/src/app/requirements.txt
RUN pip install -r /usr/src/app/requirements.txt
 
COPY . /usr/src/app
CMD gunicorn -c gunicorn.conf.py api.wsgi:application

輸出:

[24/Aug/2018:09:45:42 +0800] <16> 10.154.53.113 "POST /api/v1/compose/ HTTP/1.0" 200 5.369574 1113 https://xxxx.com/ProductPerspective/detail" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
[24/Aug/2018:09:45:43 +0800] <14> 10.154.53.113 "POST /api/v1/fund_static/ HTTP/1.0" 200 5.868518 481 https://xxxx.com/ProductPerspective/detail" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
[24/Aug/2018:09:46:00 +0800] <334> 10.154.53.113 "POST /api/v1/adjustment/ HTTP/1.0" 200 0.845745 1398 https://xxxx.com/ProductPerspective/detail" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
[24/Aug/2018:09:46:00 +0800] <12> 10.154.53.113 "POST /api/v1/risk/ HTTP/1.0" 200 0.860662 1254 https://xxxx.com/ProductPerspective/detail" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"

參考

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 謠言止于智者,不做個傳謠言的人。造謠與嫉妒,無中生有。謠言能致人于死地,人言可畏。過去造謠對人傷害太大。社會對每個...
    我的秘密小窩閱讀 237評論 0 0
  • 男主人公傅之醉臥遠方戰(zhàn)場,女主人公文答仍守信寫信,看徐子為如何用回環(huán)恬靜筆法讓他們再次凄美相遇?!}記 在停...
    徐子為閱讀 606評論 0 2
  • 親愛的女孩,你知道,天空的藍色為什么會那么澄澈嗎。我想你知道。那,你知道為什么我看你時,眼睛是五彩斑斕的嗎。我...
    22bf81a7c9bf閱讀 366評論 2 3
  • 晚上看《夏洛特的煩惱》,電影用夢的方式讓主人公實現(xiàn)現(xiàn)實中難以實現(xiàn)的夢想,比如,名利雙收,和難忘的初戀結(jié)婚。電影在虛...
    維漪閱讀 344評論 2 11

友情鏈接更多精彩內(nèi)容