gunicorn 介紹與性能分析


閱讀此文前建議先閱讀 我的博客

 

gunicorn 是一個 python wsgi http server,只支持在 unix 系統上運行

 

安裝

gunicorn 其實是 python 的一個包,安裝方法同一般包的安裝

pip install gunicorn

也可 tar 包安裝

安裝完畢可用如下命令檢測

[root@node bin]# gunicorn -h

-h 就是 help,查看 gunicorn 命令的參數

 

gunicorn 參數

-c CONFIG    : CONFIG,配置文件的路徑,通過配置文件啟動;生產環境使用;

-b ADDRESS   : ADDRESS,ip加端口,綁定運行的主機;

-w INT, --workers INT:用於處理工作進程的數量,為正整數,默認為1;

-k STRTING, --worker-class STRTING:要使用的工作模式,默認為sync異步,可以下載eventlet和gevent並指定

--threads INT:處理請求的工作線程數,使用指定數量的線程運行每個worker。為正整數,默認為1。

--worker-connections INT:最大客戶端並發數量,默認情況下這個值為1000。

--backlog int:未決連接的最大數量,即等待服務的客戶的數量。默認2048個,一般不修改;

-p FILE, --pid FILE:設置pid文件的文件名,如果不設置將不會創建pid文件


--access-logfile FILE   :  要寫入的訪問日志目錄

--access-logformat STRING:要寫入的訪問日志格式

--error-logfile FILE, --log-file FILE  :  要寫入錯誤日志的文件目錄。

--log-level LEVEL   :   錯誤日志輸出等級。


--limit-request-line INT   :  HTTP請求頭的行數的最大大小,此參數用於限制HTTP請求行的允許大小,默認情況下,這個值為4094。值是0~8190的數字。

--limit-request-fields INT   :  限制HTTP請求中請求頭字段的數量。此字段用於限制請求頭字段的數量以防止DDOS攻擊,默認情況下,這個值為100,這個值不能超過32768

--limit-request-field-size INT  :  限制HTTP請求中請求頭的大小,默認情況下這個值為8190字節。值是一個整數或者0,當該值為0時,表示將對請求頭大小不做限制


-t INT, --timeout INT:超過這么多秒后工作將被殺掉,並重新啟動。一般設定為30秒;

--daemon: 是否以守護進程啟動,默認false;

--chdir: 在加載應用程序之前切換目錄;

--graceful-timeout INT:默認情況下,這個值為30,在超時(從接收到重啟信號開始)之后仍然活着的工作將被強行殺死;一般使用默認;

--keep-alive INT:在keep-alive連接上等待請求的秒數,默認情況下值為2。一般設定在1~5秒之間。

--reload:默認為False。此設置用於開發,每當應用程序發生更改時,都會導致工作重新啟動。

--spew:打印服務器執行過的每一條語句,默認False。此選擇為原子性的,即要么全部打印,要么全部不打印;

--check-config   :顯示現在的配置,默認值為False,即顯示。

-e ENV, --env ENV: 設置環境變量;

 

gunicorn 配置

gunicorn 有兩種配置方式

命令行

示例如下

gunicorn -w 8 -b 0.0.0.0:5002 simple_flask:app

simple_flask 是 flask 的主文件

app 是 主文件中那個 app flask 對象

 

配置文件啟動

# gunicorn.conf

# 並行工作進程數
workers = 4
# 指定每個工作者的線程數
threads = 2
# 監聽內網端口5000
bind = '127.0.0.1:5000'
# 設置守護進程,將進程交給supervisor管理
daemon = 'false'
# 工作模式協程
worker_class = 'gevent'
# 設置最大並發量
worker_connections = 2000
# 設置進程文件目錄
pidfile = '/var/run/gunicorn.pid'
# 設置訪問日志和錯誤信息日志路徑
accesslog = '/var/log/gunicorn_acess.log'
errorlog = '/var/log/gunicorn_error.log'
# 設置日志記錄水平
loglevel = 'warning'

啟動 gunicorn

gunicorn -c gunicorn.conf app:app

 

gunicorn VS 自帶服務

 

flask 項目

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, World!"


if __name__ == '__main__':
    app.run(host='0.0.0.0')

 

flask 自帶服務器啟動 web

對其進行壓力測試,模擬 200 個用戶發起 9000 個請求

ab -n 9000 -c 200 -r "http://172.16.89.80:5000/"

輸出

Server Software:        Werkzeug/0.16.0
Server Hostname:        172.16.89.80
Server Port:            5000

Document Path:          /
Document Length:        13 bytes

Concurrency Level:      200
Time taken for tests:   13.862 seconds
Complete requests:      9000
Failed requests:        0
Write errors:           0
Total transferred:      1503000 bytes
HTML transferred:       117000 bytes
Requests per second:    649.26 [#/sec] (mean)
Time per request:       308.043 [ms] (mean)
Time per request:       1.540 [ms] (mean, across all concurrent requests)
Transfer rate:          105.89 [Kbytes/sec] received

Complete requests 9000 個請求;

Time taken for tests 共耗時 13.862 s;

Requests per second 每秒處理請求 649.26 個;   649.26 x 13.862 = 9000.04

 

注意 200 個用戶(並發)並不是說並發量是 200,因為一個用戶可能 狂點,在一個 request-response 沒結束之前,狂點多次請求,這也是並發,所以上述輸出表明 並發量 為 649

 

gunicorn 啟動 flask

gunicorn -w 8 -b 0.0.0.0:5002 simple_flask:app

壓力測試,同樣模擬 200 個用戶 發起 9000 個請求

/usr/bin/ab -n 9000 -c 200 -r -k  'http://172.16.89.80:5002/'

輸出

Concurrency Level:      200
Time taken for tests:   1.998 seconds
Complete requests:      9000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    0
Total transferred:      1557000 bytes
HTML transferred:       117000 bytes
Requests per second:    4503.48 [#/sec] (mean)
Time per request:       44.410 [ms] (mean)
Time per request:       0.222 [ms] (mean, across all concurrent requests)
Transfer rate:          760.84 [Kbytes/sec] received

類比上面來看本次輸出,很明顯,效率高;

並發量 4503,將近 8 倍

有意思的是我們讓 gunicorn 開了 8個 進程,所以可以這么理解, gunicorn 效率 = 單進程效率(自帶服務器)x 進程數

當然 這也要跟 硬件 有關系,如果你只是 8 核 的服務器,開了 80 個進程,就不能這么算了

 

用 gevent 並發量更大

gunicorn -k gevent -w 8 -b 0.0.0.0:5002 simple_flask:app

並發量 4890

 

gunicorn VS nginx

測試樣例還是上面那個 flask 項目

這里只做簡單分析,因為測試時跟硬件有一定關系

 

gunicorn 性能

Concurrency Level:      10000
Time taken for tests:   25.494 seconds
Complete requests:      100000
Failed requests:        292
   (Connect: 0, Receive: 88, Length: 116, Exceptions: 88)
Write errors:           0
Total transferred:      17279932 bytes
HTML transferred:       1298492 bytes
Requests per second:    3922.52 [#/sec] (mean)
Time per request:       2549.384 [ms] (mean)
Time per request:       0.255 [ms] (mean, across all concurrent requests)
Transfer rate:          661.92 [Kbytes/sec] received

失敗 292 次請求,並發 3922

 

nginx 性能

Concurrency Level:      10000
Time taken for tests:   26.333 seconds
Complete requests:      100000
Failed requests:        42
   (Connect: 0, Receive: 0, Length: 42, Exceptions: 0)
Write errors:           0
Total transferred:      17292734 bytes
HTML transferred:       1299454 bytes
Requests per second:    3797.45 [#/sec] (mean)
Time per request:       2633.344 [ms] (mean)
Time per request:       0.263 [ms] (mean, across all concurrent requests)
Transfer rate:          641.29 [Kbytes/sec] received

失敗 42 次請求,並發 3797

 

nginx 性能不差,重要的是穩定。

 

 

 

參考資料:

https://www.cnblogs.com/cwp-bg/p/8780204.html  python之gunicorn的配置

https://blog.csdn.net/y472360651/article/details/78538188  Gunicorn-配置詳解

https://www.jianshu.com/p/69e75fc3e08e  gunicorn 詳解

https://blog.csdn.net/bbwangj/article/details/82684573  gunicorn簡介、架構、安裝與配置

https://www.jianshu.com/p/b97f80a630db

https://blog.51cto.com/7613336/2074032  優雅的退出/關閉/重啟gunicorn進程


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM