config
參數名 | 參數內容 | 默認 |
---|---|---|
-c , --config | config_file | None |
配置文件路徑,路徑形式的字符
使用config參數及配置文件啓動,例子:
gunicorn -c gunicorn.conf manager:app
配置文件中的參數
bind
參數名 | 參數內容 | 默認 |
---|---|---|
bind | ip:port | [‘127.0.0.1:8000’] |
監聽地址和端口
例子:
bind = "%s:%s" % ("0.0.0.0", 9102)
進程相關
workers
參數名 | 參數內容 | 默認 |
---|---|---|
workers | 進程數量 | 1 |
用於處理工作進程的數量
worker推薦的數量爲當前的CPU個數*2 + 1。
計算當前的CPU個數方法並設置:
import multiprocessing
workers = multiprocessing.cpu_count() * 2
worker_class
參數名 | 參數內容 | 默認 |
---|---|---|
worker_class | 工作模式 | sync |
多個工作模式:
同步Worker:sync 默認模式,也就是一次只處理一個請求
異步Worker:通過Eventlet、Gevent實現的異步模式
異步IO Worker:目前支持gthread和gaiohttp兩種類型
例子:
worker_class = 'gevent'
worker_connections
參數名 | 參數內容 | 默認 |
---|---|---|
worker_connections | 客戶端最大同時連接數 | 1000 |
使用於gevent和eventlet工作模式
例子:
worker_connections = 5000
threads
參數名 | 參數內容 | 默認 |
---|---|---|
threads | 線程數 | 1 |
工作進程中線程的數,建議值2-4 x CPU核心數
此配置只適用於gthread 進程工作方式, 因爲gevent這種使用的是協程工作方式。
max_requests
參數名 | 參數內容 | 默認 |
---|---|---|
max_requests | 最大請求數 | 0 |
重啓之前,worker將處理的最大請求數
0,默認值時,則禁止了worker的自動重啓
這個方法主要是防止內存泄露
當超過max_requests時,就會重啓worker
阿里雲服務器TCP連接數(Count):
max_requests_jitter
參數名 | 參數內容 | 默認 |
---|---|---|
max_requests_jitter | max_requests的最大抖動值 | 0 |
抖動將導致每個工作的重啓被隨機化,
這是爲了避免所有工作被重啓。randint(0,max-requests-jitter)
timeout
參數名 | 參數內容 | 默認 |
---|---|---|
timeout | 秒數 | 30秒 |
worker超時時間,超時重啓
graceful_timeout
參數名 | 參數內容 | 默認 |
---|---|---|
graceful_timeout | 秒數 | 接收到restart信號後,worker可以在graceful_timeout時間內,繼續處理完當前requests |
keepalive
參數名 | 參數內容 | 默認 |
---|---|---|
keepalive | INT | 連接上等待請求的秒數,默認情況下值爲2。一般設定在1~5秒之間。 |
安全
limit_request_line
參數名 | 參數內容 | 默認 |
---|---|---|
limit_request_line | 最大數 | 4094 |
http request line最大字節數。值範圍0-8190, 0表示無限制。
limit_request_fields
參數名 | 參數內容 | 默認 |
---|---|---|
limit_request_fields | 最大值 | 100 |
http request中 header字段數的最大值,最大32768
limit_request_field_size
參數名 | 參數內容 | 默認 |
---|---|---|
limit_request_field_size | 最大值 | 8190 |
http request header字段最大字節數,0時無限制
調試
reload
參數名 | 參數內容 | 默認 |
---|---|---|
reload | True/False | False |
當代碼有修改時,自動重啓workers。適用於開發環境。
reload_extra_files
參數名 | 參數內容 | 默認 |
---|---|---|
reload_extra_files | str | [ ] |
擴展reload配置,增加templates,configurations等文件修改監控。
spew
參數名 | 參數內容 | 默認 |
---|---|---|
spew | True/False | False |
check_config
參數名 | 參數內容 | 默認 |
---|---|---|
check_config | True/False | False |
檢查配置
server 機制
chdir
參數名 | 參數內容 | 默認 |
---|---|---|
chdir | 文件路徑 | /home/docs/checkouts/readthedocs.org/user_builds/gunicorn-docs/checkouts/latest/docs/source |
在app加載之前,進入到此目錄
配合使用效果更佳:
path_of_current_file = os.path.abspath(__file__)
path_of_current_dir = os.path.split(path_of_current_file)[0]
chdir = path_of_current_dir
daemon
參數名 | 參數內容 | 默認 |
---|---|---|
daemon | True/False | False |
應用是否以daemon方式運行。
raw_env
參數名 | 參數內容 | 默認 |
---|---|---|
raw_env | key=value, 傳遞環境參數。 | [ ] |
pidfile
參數名 | 參數內容 | 默認 |
---|---|---|
pidfile | 文件路徑 | None |
存儲pid的文件路徑
worker_tmp_dir
參數名 | 參數內容 | 默認 |
---|---|---|
worker_tmp_dir | 路徑 | None |
臨時工作目錄
user
參數名 | 參數內容 | 默認 |
---|---|---|
user | 用戶名或者用戶id | 1005 |
指定worker進程的運行用戶名
日誌
accesslog
參數名 | 參數內容 | 默認 |
---|---|---|
accesslog | 文件路徑 | None |
接收日誌文件路徑
access_log_format
參數名 | 參數內容 | 默認 |
---|---|---|
access_log_format | 日誌格式 | %(h)s %(l)s %(u)s %(t)s “%®s” %(s)s %(b)s “%(f)s” “%(a)s” |
文檔:
https://docs.gunicorn.org/en/latest/settings.html#access-log-format
errorlog
參數名 | 參數內容 | 默認 |
---|---|---|
errorlog | 文件路徑 | - |
錯誤日誌路徑
loglever
參數名 | 參數內容 | 默認 |
---|---|---|
loglever | str | info |
日誌等級:debug, info, warning, error, critical.
capture_output
重定向stdout/stderr到error log file。
logger_class
日誌實現類。缺省gunicorn.glogging.Logger 。
logconfig
日誌配置文件。同python標準日誌模塊logging的配置。
內容整合來自:
https://docs.gunicorn.org/en/latest/settings.html