Gunicorn運(yùn)行與配置方法
Gunicorn“綠色獨(dú)角獸”是一個(gè)被廣泛使用的高性能的Python WSGI UNIX HTTP服務(wù)器,移植自Ruby的獨(dú)角獸(Unicorn )項(xiàng)目,使用pre-fork worker模式,具有使用非常簡(jiǎn)單,輕量級(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)程在處理請(qǐng)求時(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ò)誤日志文件
總結(jié)
以上所述是小編給大家介紹的Gunicorn運(yùn)行與配置方法,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
linux服務(wù)器下通過iptables+Denyhost抵御暴力破解的配置方法
這篇文章主要介紹了linux服務(wù)器下通過iptables + Denyhost抵御暴力破解的方法,需要的朋友可以參考下2014-11-11
linux后臺(tái)執(zhí)行命令&和nohup的具體使用方法
這篇文章主要介紹了linux后臺(tái)執(zhí)行命令&和nohup的具體使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Linux進(jìn)程間通信方式之socket使用實(shí)例
這篇文章主要介紹了Linux進(jìn)程間通信方式之socket使用實(shí)例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
在Linux中使用MD5實(shí)現(xiàn)用戶驗(yàn)證的解決方法
本篇文章小編為大家介紹,在Linux中使用MD5實(shí)現(xiàn)用戶驗(yàn)證的解決方法。需要的朋友參考下2013-04-04
Linux下gdb調(diào)試打印進(jìn)程內(nèi)存信息方式
這篇文章主要介紹了Linux下gdb調(diào)試打印進(jìn)程內(nèi)存信息方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09

