Gunicorn是一個(gè)被廣泛使用的高性能的Python WSGI UNIX HTTP服務(wù)器,移植至Ruby的獨(dú)角獸(Unicorn)項(xiàng)目弃锐,使用pre-fork worker模式,具有使用非常簡(jiǎn)單霹菊,輕量級(jí)的資源消耗,以及高性能等特點(diǎn)鸠按。
Django自帶的簡(jiǎn)易服務(wù)器饶碘,它是一個(gè)純Python寫的輕量級(jí)的WEB服務(wù)器,但它是為了開發(fā)而設(shè)計(jì)的扎运,不適合在生產(chǎn)環(huán)境中部署饮戳。
python manage.py runserver 0.0.0.0:8000
在Django中配置gunicorn洞拨,適合高并發(fā)的生產(chǎn)環(huán)境。
特點(diǎn)
Gunicorn是基于prefork模式的Python wsgi應(yīng)用服務(wù)器烦衣,支持 Unix like的系統(tǒng)
采用epoll (Linux下) 非阻塞網(wǎng)絡(luò)I/O 模型
多種Worker類型可以選擇 同步的,基于事件的(gevent tornado等)启泣,基于多線程的
高性能示辈,比之uwsgi不相上下
配置使用非常簡(jiǎn)單
支持 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)聽隊(duì)列數(shù)量矾麻,64-2048
#chdir = '/home/test/server/bin' #gunicorn要切換到的目的工作目錄
worker_class = 'sync' #使用gevent模式,還可以使用sync 模式险耀,默認(rèn)的是sync模式
workers = 4 # multiprocessing.cpu_count() #進(jìn)程數(shù)
threads = 16 #multiprocessing.cpu_count()*4 #指定每個(gè)進(jìn)程開啟的線程數(shù)
loglevel = 'info' #日志級(jí)別弄喘,這個(gè)日志級(jí)別指的是錯(cuò)誤日志的級(jí)別,而訪問日志的級(jí)別無法設(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" #錯(cuò)誤日志文件
accesslog = "-" #訪問日志文件甩牺,"-" 表示標(biāo)準(zhǔn)輸出
errorlog = "-" #錯(cuò)誤日志文件蘑志,"-" 表示標(biāo)準(zhǔn)輸出
proc_name = 'fof_api' #進(jìn)程名
其中access_log_format選項(xiàng)的變量含義如下:
其每個(gè)選項(xiàng)的含義如下:
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日志文件可以寫到文件贬派,也可以標(biāo)準(zhǔn)輸出到屏幕急但。
正常啟動(dòng)
以gunicorn.conf.py配置文件啟動(dòng)gunicorn
gunicorn -c gunicorn.conf.py api.wsgi:application
也可以直接啟動(dòng)
gunicorn api.wsgi:application -b 0.0.0.0:8091 -w 4 -k gthread
-w 4是啟動(dòng)4個(gè)進(jìn)程,可以支持更多的并發(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方式啟動(dòng)
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"