使用flask框架進行web開發的時候,默認情況是單一的進程。當出現併發請求,就會出現排隊現象。
解決:
- 開發環境, threaded=True
if __name__ == '__main__': # 多進程與多線程不能同時開啓 #app.run('0.0.0.0', port=5001, threaded=False, processes=3) # 適用多進程啓動服務 #app.run('0.0.0.0', port=5001, threaded=True) # 適用多線程啓動服務
- 生產環境,使用gunicorn部署
python常見的web部署搭配nginx+gunicorn。
一、gunicorn安裝
pip install gunicorn
二、配置參數詳解
參考:
1、Gunicorn配置部分的翻譯
2、Gunicorn-配置詳解
3、python之gunicorn的配置
4、gunicorn官方文檔
具體參數如下:
[2019-07-04 19:37:21 +0800] [31677] [DEBUG] Current configuration:
config: /home/gunicorn/config.py
bind: ['172.28.171.98:8080']
backlog: 2048
workers: 1
worker_class: sync
threads: 1
worker_connections: 1000
max_requests: 0
max_requests_jitter: 0
timeout: 600
graceful_timeout: 30
keepalive: 2
limit_request_line: 4094
limit_request_fields: 100
limit_request_field_size: 8190
reload: True
reload_engine: auto
reload_extra_files: []
spew: False
check_config: False
preload_app: False
sendfile: None
reuse_port: False
chdir: /home/260190/PycharmProjects/文檔內容修訂檢測
daemon: False
raw_env: []
pidfile: None
worker_tmp_dir: None
user: 1000
group: 1000
umask: 0
initgroups: False
tmp_upload_dir: None
secure_scheme_headers: {'X-FORWARDED-PROTOCOL': 'ssl', 'X-FORWARDED-PROTO': 'https', 'X-FORWARDED-SSL': 'on'}
forwarded_allow_ips: ['127.0.0.1']
accesslog: /home/260190/PycharmProjects/文檔內容修訂檢測/gunicorn/access.log
disable_redirect_access_to_syslog: False
access_log_format: %(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"
errorlog: /home/260190/PycharmProjects/文檔內容修訂檢測/gunicorn/error.log
loglevel: debug
capture_output: True
logger_class: gunicorn.glogging.Logger
logconfig: None
logconfig_dict: {}
syslog_addr: udp://localhost:514
syslog: False
syslog_prefix: None
syslog_facility: user
enable_stdio_inheritance: False
statsd_host: None
statsd_prefix:
proc_name: None
default_proc_name: main:app
pythonpath: None
paste: None
on_starting: <function OnStarting.on_starting at 0x7fc33c6579d8>
on_reload: <function OnReload.on_reload at 0x7fc33c657ae8>
when_ready: <function WhenReady.when_ready at 0x7fc33c657bf8>
pre_fork: <function Prefork.pre_fork at 0x7fc33c657d08>
post_fork: <function Postfork.post_fork at 0x7fc33c657e18>
post_worker_init: <function PostWorkerInit.post_worker_init at 0x7fc33c657f28>
worker_int: <function WorkerInt.worker_int at 0x7fc33c47e0d0>
worker_abort: <function WorkerAbort.worker_abort at 0x7fc33c47e1e0>
pre_exec: <function PreExec.pre_exec at 0x7fc33c47e2f0>
pre_request: <function PreRequest.pre_request at 0x7fc33c47e400>
post_request: <function PostRequest.post_request at 0x7fc33c47e488>
child_exit: <function ChildExit.child_exit at 0x7fc33c47e598>
worker_exit: <function WorkerExit.worker_exit at 0x7fc33c47e6a8>
nworkers_changed: <function NumWorkersChanged.nworkers_changed at 0x7fc33c47e7b8>
on_exit: <function OnExit.on_exit at 0x7fc33c47e8c8>
proxy_protocol: False
proxy_allow_ips: ['127.0.0.1']
keyfile: None
certfile: None
ssl_version: 2
cert_reqs: 0
ca_certs: None
suppress_ragged_eofs: True
do_handshake_on_connect: False
ciphers: TLSv1
raw_paste_global_conf: []
[2019-07-04 19:37:21 +0800] [31677] [INFO] Starting gunicorn 19.9.0
[2019-07-04 19:37:21 +0800] [31677] [DEBUG] Arbiter booted
[2019-07-04 19:37:21 +0800] [31677] [INFO] Listening at: http://172.28.171.98:8080 (31677)
[2019-07-04 19:37:21 +0800] [31677] [INFO] Using worker: sync
[2019-07-04 19:37:21 +0800] [31680] [INFO] Booting worker with pid: 31680
[2019-07-04 19:37:21 +0800] [31677] [DEBUG] 1 workers
[2019-07-04 19:37:41 +0800] [31680] [DEBUG] worker: SIGWINCH ignored.
[2019-07-04 19:37:41 +0800] [31677] [INFO] Handling signal: winch
[2019-07-04 19:37:41 +0800] [31677] [DEBUG] SIGWINCH ignored. Not daemonized
三、項目中的配置文件config.py
:
有的也寫成gunicorn.conf
,應該是一樣的
import multiprocessing
bind = "127.0.0.1:5000"
# workers = multiprocessing.cpu_count() * 2 + 1
workers = 10
accesslog = '/home/gunicorn/access.log' # 訪問日誌目錄
errorlog = '/home/gunicorn/error.log' # 錯誤日誌目錄
capture_output = True # 重定向標準輸出到錯誤日誌。默認爲False。
timeout = 600 # 過期時間
loglevel = "debug" # 日誌級別,這個日誌級別指的是錯誤日誌的級別,而訪問日誌的級別無法設置
# debug、info、warning、error、critical
reload = True # 重載 更改代碼的時候重啓workers, 只建議在開發過程中開啓。
daemon = True # 以守護進程形式來運行Gunicorn進程。其實就是將這個服務放到後臺去運行。默認爲False。
啓動:gunicorn -c config.py main:app
或者 gunicorn -c gunicorn.conf main:app
三、總結
命令行運行 gunicorn -c config.py main:app
,開啓10進程,超時時間爲10min,服務後臺運行,重定向標準輸出到錯誤日誌,輸出錯誤日誌,訪問日誌等。解決了之前總是用nohup python main.py >out.log 2>&1 &來啓動Flask後臺服務的問題。