Skip to content

Gunicorn

测试环境

当使用flask run启动服务时,提示这是开发服务

shell
flask run --host=0.0.0.0 --port=9999

输出

shell
 * Serving Flask app '1hellowold.py'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:9999
 * Running on http://192.168.1.4:9999
Press CTRL+C to quit

Gunicorn

Gunicorn是一个Python WSGI HTTP服务器,是一个预先叉生成的,用于UNIX的WSGI HTTP服务器。它既是一个独立的服务器,也可以作为应用服务器网关接口(ASGI)服务器。

启动应用

方式1:基本启动

shell
gunicorn wsgi:application

方式2:使用配置文件启动

shell
gunicorn -c gunicorn_config.py wsgi:application

输出

shell
[2025-01-18 17:46:10 +0800] [65598] [INFO] Starting gunicorn 23.0.0
[2025-01-18 17:46:10 +0800] [65598] [INFO] Listening at: http://127.0.0.1:8000 (65598)
[2025-01-18 17:46:10 +0800] [65598] [INFO] Using worker: sync
[2025-01-18 17:46:10 +0800] [65600] [INFO] Booting worker with pid: 65600

方式3:使用命令行启动

shell
gunicorn --workers=4 --bind=0.0.0.0:8000 wsgi:application

输出

shell
[2025-01-18 17:48:33 +0800] [65731] [INFO] Starting gunicorn 23.0.0
[2025-01-18 17:48:33 +0800] [65731] [INFO] Listening at: http://0.0.0.0:8000 (65731)
[2025-01-18 17:48:33 +0800] [65731] [INFO] Using worker: sync
[2025-01-18 17:48:33 +0800] [65733] [INFO] Booting worker with pid: 65733
[2025-01-18 17:48:33 +0800] [65734] [INFO] Booting worker with pid: 65734
[2025-01-18 17:48:33 +0800] [65735] [INFO] Booting worker with pid: 65735
[2025-01-18 17:48:33 +0800] [65736] [INFO] Booting worker with pid: 65736

停止应用

shell
pkill -f gunicorn

输出

shell
[2025-01-18 17:51:37 +0800] [65731] [INFO] Handling signal: term
[2025-01-18 17:51:37 +0800] [65733] [INFO] Worker exiting (pid: 65733)
[2025-01-18 17:51:37 +0800] [65735] [INFO] Worker exiting (pid: 65735)
[2025-01-18 17:51:37 +0800] [65734] [INFO] Worker exiting (pid: 65734)
[2025-01-18 17:51:37 +0800] [65736] [INFO] Worker exiting (pid: 65736)
[2025-01-18 17:51:37 +0800] [65731] [INFO] Shutting down: Master