溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Gunicorn運(yùn)行與配置的示例分析

發(fā)布時(shí)間:2021-08-20 14:18:43 來(lái)源:億速云 閱讀:174 作者:小新 欄目:服務(wù)器

這篇文章主要為大家展示了“Gunicorn運(yùn)行與配置的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“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)程形式來(lái)運(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ù)先f(wàn)ork出指定數(shù)量的worker進(jìn)程在處理請(qǐng)求時(shí),gunicorn依靠操作系統(tǒng)來(lái)提供負(fù)載均衡,通常推薦的worker數(shù)量是:(2 x $num_cores) + 1

-k WORKERCLASS, --worker-class=WORKERCLASS

工作進(jìn)程類(lèi)型. 包括 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)要寫(xiě)入Access log的文件FILE. '-' 表示輸出到標(biāo)準(zhǔn)輸出.

--error-logfile FILE, --log-file FILE

確認(rèn)要寫(xiě)入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ù)寫(xiě)進(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)聽(tīng)隊(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)程開(kāi)啟的線(xiàn)程數(shù)
loglevel = 'info' #日志級(jí)別,這個(gè)日志級(jí)別指的是錯(cuò)誤日志的級(jí)別,而訪(fǎng)問(wèn)日志的級(jí)別無(wú)法設(shè)置
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'  #設(shè)置gunicorn訪(fǎng)問(wèn)日志格式,錯(cuò)誤日志無(wú)法設(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"   #訪(fǎng)問(wèn)日志文件
errorlog = "/home/test/server/log/gunicorn_error.log"    #錯(cuò)誤日志文件

以上是“Gunicorn運(yùn)行與配置的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI