Gunicorn“綠色獨(dú)角獸”是一個(gè)被廣泛使用的高性能的Python WSGI UNIX HTTP服務(wù)器,移植自Ruby的獨(dú)角獸(Unicorn )項(xiàng)目,使用pre-fork worker模式,具有使用非常簡單,輕量級(jí)的資源消耗,以及高性能等特點(diǎn)。
安裝gunicorn:
$ sudo apt-get update $ sudo apt-get install gunicorn
運(yùn)行g(shù)unicorn:
$ gunicorn [OPTIONS] 模塊名:變量名
模塊名是python文件名,可以是完整的路徑+python文件名;變量名是python文件中可調(diào)用的WSGI(Web Server Gateway ).
示例:
# filename:test.py def app(environ, start_response): """Simplest possible application object""" data = 'Hello, World!\n' status = '200 OK' response_headers = [ ('Content-type','text/plain'), ('Content-Length', str(len(data))) ] start_response(status, response_headers) return iter([data])
運(yùn)行app:
$ gunicorn --workers=2 test:app
常用配置參數(shù):
-c CONFIG, --config=CONFIG
指定一個(gè)配置文件(py文件).
-b BIND, --bind=BIND
與指定socket進(jìn)行綁定.
-D, --daemon
以守護(hù)進(jìn)程形式來運(yùn)行Gunicorn進(jìn)程,其實(shí)就是將這個(gè)服務(wù)放到后臺(tái)去運(yùn)行。
-w WORKERS, --workers=WORKERS
工作進(jìn)程的數(shù)量。上邊提到gunicorn是一個(gè)pre-fork worker模式,就是指gunicorn啟動(dòng)的時(shí)候,在主進(jìn)程中會(huì)預(yù)先fork出指定數(shù)量的worker進(jìn)程在處理請求時(shí),gunicorn依靠操作系統(tǒng)來提供負(fù)載均衡,通常推薦的worker數(shù)量是:(2 x $num_cores) + 1
-k WORKERCLASS, --worker-class=WORKERCLASS
工作進(jìn)程類型. 包括 sync(默認(rèn)), eventlet, gevent, or tornado, gthread, gaiohttp.
--backlog INT
最大掛起的連接數(shù).
--chdir
切換到指定的工作目錄.
--log-level LEVEL
輸出error log的顆粒度,有效的LEVEL有:
debug
info
warning
error
critical
--access-logfile FILE
確認(rèn)要寫入Access log的文件FILE. '-' 表示輸出到標(biāo)準(zhǔn)輸出.
--error-logfile FILE, --log-file FILE
確認(rèn)要寫入Error log的文件FILE. '-' 表示輸出到標(biāo)準(zhǔn)錯(cuò)誤輸出.
gunicorn配置
Gunicorn從三個(gè)不同地方獲取配置:
框架設(shè)置(通常只影響到Paster應(yīng)用)
配置文件(python文件):配置文件中的配置會(huì)覆蓋框架的設(shè)置。
命令行
框架設(shè)置只跟Paster(一個(gè)Web框架)有關(guān),不討論;命令行配置如上部分所示;現(xiàn)在我們看下怎么用配置文件配置gunicorn:
配置文件必須是一個(gè)python文件,只是將命令行中的參數(shù)寫進(jìn)py文件中而已,如果需要設(shè)置哪個(gè)參數(shù),則在py文件中為該參數(shù)賦值即可。例如:
# example.py bind = "127.0.0.1:8000" workers = 2
運(yùn)行g(shù)unicorn:
$ gunicorn -c example.py test:app
等同于:
$ gunicorn -w 2 -b 127.0.0.1:8000 test:app
當(dāng)然,配置文件還能實(shí)現(xiàn)更復(fù)雜的配置:
# gunicorn.py import logging import logging.handlers from logging.handlers import WatchedFileHandler import os import multiprocessing bind = '127.0.0.1:8000' #綁定ip和端口號(hào) backlog = 512 #監(jiān)聽隊(duì)列 chdir = '/home/test/server/bin' #gunicorn要切換到的目的工作目錄 timeout = 30 #超時(shí) worker_class = 'gevent' #使用gevent模式,還可以使用sync 模式,默認(rèn)的是sync模式 workers = multiprocessing.cpu_count() * 2 + 1 #進(jìn)程數(shù) threads = 2 #指定每個(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"' #設(shè)置gunicorn訪問日志格式,錯(cuò)誤日志無法設(shè)置 """ 其每個(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 = "/home/test/server/log/gunicorn_access.log" #訪問日志文件 errorlog = "/home/test/server/log/gunicorn_error.log" #錯(cuò)誤日志文件
參考官網(wǎng)
總結(jié)
以上所述是小編給大家介紹的Gunicorn運(yùn)行與配置方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
標(biāo)簽:德宏 泰安 福州 安康 淮安 上海 樂山 鷹潭
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Gunicorn運(yùn)行與配置方法》,本文關(guān)鍵詞 Gunicorn,運(yùn)行,與,配置,方法,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。