gunicorn

安裝

pip install gunicorn

常用命令

gunicorn -w 4 -b 127.0.0.1:8080 -D --access-logfile=/var/log/gunicorn.log 項目名.wsgi:application
1)-c CONFIG,–config=CONFIG
指定一個配置文件(py文件)
2)-b BIND,–bind=BIND
與指定socket進行板頂
3)-D,–daemon
後臺進程方式運行gunicorn進程
4)-w WORKERS,–workers=WORKERS
工作進程的數量
5)-k WORKERCLASS,–worker-class=WORKERCLASS
工作進程類型,包括sync(默認),eventlet,gevent,tornado,gthread,gaiohttp
6)–backlog INT
最大掛起的連接數
7)–log-level LEVEL
日誌輸出等級
8)–access-logfile FILE
訪問日誌輸出文件
9)–error-logfile FILE
錯誤日誌輸出文件

使用配置文件啓動項目
# gunicorn.conf.py
 import logging
 import logging.handlers
 from logging.handlers import WatchedFileHandler
 import os
 import multiprocessing
 
 bind = '127.0.0.1:8000'  # 綁定ip和端口號
 backlog = 512  # 監聽隊列
 chdir = '/home/python/PycharmProjects/News-Information'  # gunicorn要切換到的目的工作目錄
 timeout = 30  # 超時
 worker_class = 'gevent'  # 使用gevent模式,還可以使用sync 模式,默認的是sync模式
 
 workers = multiprocessing.cpu_count() * 2 + 1  # 進程數
 threads = 2  # 指定每個進程開啓的線程數
 loglevel = 'info'  # 日誌級別,這個日誌級別指的是錯誤日誌的級別,而訪問日誌的級別無法設置
 access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'  # 設置gunicorn訪問日誌格式,錯誤日誌無法設置
 
 """
 其每個選項的含義如下:
 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/python/PycharmProjects/News-Information/log/gunicorn_access.log"  # 訪問日誌文件
 errorlog = "/home/python/PycharmProjects/News-Information/log/gunicorn_error.log"
詳解

server socket
bind
監聽地址和端口。

backlog
服務器中在pending狀態的最大連接數,即client處於waiting的數目。超過這個數目, client連接會得到一個error。
建議值64-2048。

worker 進程
workers
worker進程的數量。建議值2-4 x $(NUM_CORES), 缺省爲1。

worker_class
worker進程的工作方式。 有 sync, eventlet, gevent, tornado, gthread, 缺省值sync。

threads
工作進程中線程的數量。建議值2-4 x $(NUM_CORES), 缺省值1。
此配置只適用於gthread 進程工作方式, 因爲gevent這種使用的是協程工作方式。

worker_connections
客戶端最大同時連接數。只適用於eventlet, gevent工作方式。

max_requests
worker重啓之前處理的最大requests數, 缺省值爲0表示自動重啓disabled。主要是防止內存泄露。

max_requests_jitter
抖動參數,防止worker全部同時重啓。

timeout
通常設爲30。

graceful_timeout
接收到restart信號後,worker可以在graceful_timeout時間內,繼續處理完當前requests。

keepalive
server端保持連接時間。

security
limit_request_line
http request line最大字節數。值範圍0-8190, 0表示無限制。

limit_request_field
http request中 header字段數的最大值。缺省爲100,最大32768。

limit_request_field_size
http request header字段最大字節數。0表示無限制。

調試
reload
當代碼有修改時,自動重啓workers。適用於開發環境。

reload_extra_files
擴展reload配置,增加templates,configurations等文件修改監控。

spew
跟蹤程序執行的每一行。

check_config
檢查配置。

server 機制
sendfile
系統底層拷貝數據方式,提供performance。

chdir
在app加載之前,進入到此目錄。

daemon
應用是否以daemon方式運行。

raw_env
key=value, 傳遞環境參數。

pidfile
pid存儲文件路徑。

worker_tmp_dir
臨時工作目錄。

user
指定worker進程的運行用戶名。

group
指定worker進程運行用戶所在組。

umask
gunicorn創建文件的缺省權限。

pythonpath
附加到python path的目錄列表。

日誌
accesslog
訪問日誌文件路徑。

access_log_format
日誌格式。 例如 %(h)s %(l)s %(u)s %(t)s “%®s” %(s)s %(b)s “%(f)s” “%(a)s” 。

errorlog
錯誤日誌路徑。

loglever
日誌級別。debug, info, warning, error, critical.

capture_output
重定向stdout/stderr到error log file。

logger_class
日誌實現類。缺省gunicorn.glogging.Logger 。

logconfig
日誌配置文件。同python標準日誌模塊logging的配置。

進程名
proc_name
設置進程名(setproctitle),在ps,top等命令中會看到. 缺省值爲default_proc_name配置。
server鉤子
on_starting
on_reload
when_ready
pre_fork
post_fork
post_worker_init
worker_init
worker_abort
pre_exec
pre_request
post_request
child_exit
worker-exit
nworkers_changed
on_exit

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章