欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Gunicorn運(yùn)行與配置方法

 更新時間:2019年08月04日 09:59:42   作者:涵仔睡覺  
這篇文章主要介紹了Gunicorn運(yùn)行與配置方法,使用pre-fork worker模式,具有使用非常簡單,輕量級的資源消耗,以及高性能等特點(diǎn)。對此感興趣的朋友跟隨小編一起看看吧

Gunicorn“綠色獨(dú)角獸”是一個被廣泛使用的高性能的Python WSGI UNIX HTTP服務(wù)器,移植自Ruby的獨(dú)角獸(Unicorn )項(xiàng)目,使用pre-fork worker模式,具有使用非常簡單,輕量級的資源消耗,以及高性能等特點(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

指定一個配置文件(py文件).

-b BIND, --bind=BIND

與指定socket進(jìn)行綁定.

-D, --daemon

以守護(hù)進(jìn)程形式來運(yùn)行Gunicorn進(jìn)程,其實(shí)就是將這個服務(wù)放到后臺去運(yùn)行。

-w WORKERS, --workers=WORKERS

工作進(jìn)程的數(shù)量。上邊提到gunicorn是一個pre-fork worker模式,就是指gunicorn啟動的時候,在主進(jìn)程中會預(yù)先fork出指定數(shù)量的worker進(jìn)程在處理請求時,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)錯誤輸出.

gunicorn配置

Gunicorn從三個不同地方獲取配置:

框架設(shè)置(通常只影響到Paster應(yīng)用)

配置文件(python文件):配置文件中的配置會覆蓋框架的設(shè)置。

命令行

框架設(shè)置只跟Paster(一個Web框架)有關(guān),不討論;命令行配置如上部分所示;現(xiàn)在我們看下怎么用配置文件配置gunicorn:

配置文件必須是一個python文件,只是將命令行中的參數(shù)寫進(jìn)py文件中而已,如果需要設(shè)置哪個參數(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和端口號
backlog = 512        #監(jiān)聽隊(duì)列
chdir = '/home/test/server/bin' #gunicorn要切換到的目的工作目錄
timeout = 30   #超時
worker_class = 'gevent' #使用gevent模式,還可以使用sync 模式,默認(rèn)的是sync模式
workers = multiprocessing.cpu_count() * 2 + 1  #進(jìn)程數(shù)
threads = 2 #指定每個進(jìn)程開啟的線程數(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"'  #設(shè)置gunicorn訪問日志格式,錯誤日志無法設(shè)置
"""
其每個選項(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"    #錯誤日志文件

參考官網(wǎng)

總結(jié)

以上所述是小編給大家介紹的Gunicorn運(yùn)行與配置方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!

相關(guān)文章

最新評論