Article structure:
- Gunicorn
- preliminary
- install and usage
- pros & cons
- supervisor
- install and usage
Gunicorn
Gunicorn ‘Green Unicorn’(发音 jee-unicorn | green unicorn | gun-i-corn) 是一个被广泛使用的 Python WSGI UNIX HTTP 服务器,移植自 Ruby 的独角兽(Unicorn )项目,采用 pre-fork 工作模式,使用简单,资源消耗少且高效。gunicorn 依靠操作系统来提供负载均衡,其源码中使用了 fcntl、os.fork 等只在 Unix 中存在的模块和接口,因此当前版本(v20.1.0)在不使用补丁程序情况下,只能在 Unix 环境下运行。对于 Windows 环境,可使用 waitress 来运行 web 服务。
Usage
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "SUCCESS"
if __name__ == "__main__":
app.debug = True
app.run()
对于以上 wsgi.py 文件,使用 gunicorn 监听请求:
gunicorn -w 2 -b 0.0.0.0:8000 wsgi:app
参数说明:
-w,指定worker进程的数量-b,指定监听地址/端口
gunicorn -h 查阅常用参数
usage: gunicorn [OPTIONS] [APP_MODULE]
optional arguments:
-h, --help 帮助信息
-v, --version 版本
-c CONFIG, --config CONFIG
通过配置文件 CONFIG 运行
-b ADDRESS, --bind ADDRESS
监听地址与端口 [['127.0.0.1:8000']]
--backlog INT 最大挂起的连接数
-w INT, --workers INT
工作进程的最大数量
-k STRING, --worker-class STRING
使用的 worker 的类型,默认 sync
--threads INT 工作线程的最大数量
--worker-connections INT
The maximum number of simultaneous clients. [1000]
--max-requests INT The maximum number of requests a worker will process before restarting. [0]
--max-requests-jitter INT
The maximum jitter to add to the *max_requests* setting. [0]
-t I

本文介绍了如何使用Gunicorn(一个Python WSGI服务器)和Supervisor(一个进程管理工具)来部署和管理Python项目。Gunicorn采用预派生工作模式,资源消耗少且高效,但不支持自动重启和状态查看。而Supervisor可以解决这个问题,它能监控进程状态并自动重启。文章详细阐述了Gunicorn的使用、预派生工作模式、Supervisor的安装配置和使用方法。
最低0.47元/天 解锁文章
1887

被折叠的 条评论
为什么被折叠?



