您当前的位置: 首页 >  学无止境 >  文章详情

python supervisor + gunicorn + flask实现生产环境线上运行

时间: 2023-11-07 【学无止境】 阅读量:共310人围观

简介 Gunicorn是基于unix系统,被广泛应用的高性能的Python WSGI HTTP Server。用来解析HTTP请求的网关服务。supervisor:一个专门用来管理进程的工具,还可以管理系统的工具进程。

安装gunicorn、supervisor命令

# 创建虚拟环境 python -m venv venv # 然后使虚拟环境生效(windows) venv\Scripts\activate # source venv/bin/activate # (Linux激活虚拟环境) // 安装gunicorn pip install gunicorn // 安装supervisor pip install supervisor // 安装supervisor // 进入etc创建supervisor文件夹 cd etc mkdir supervisor cd supervisor // 生成配置文件 echo_supervisord_conf > supervisor.conf

supervisor.conf

// 在文件的末尾加上以下配置 files=/etc/supervisord.conf [program:flask_app] directory=/data/web #项目目录 command=/data/web/venv/bin/gunicorn -w 4 -b 0.0.0.0:8000 'applications:create_app()' #gunicorn的指令 -w 表示开启多少个 worker,-b 表示 gunicorn 开发的访问地址。 startsecs=5 #启动5秒后没有异常退出,视作正常启动 autostart=true #在supervisord启动时自动启动 autorestart=true #程序异常退出后重启 redirect_stderr=true #将错误信息重定向至stdout日志 stdout_logfile=/data/web/test.log # 进程日志

不配置supervisor后台运行启动

// 生成一个日志文件test.log gunicorn -b 127.0.0.1:8000 'applications:create_app()' > test.log 2>&1 &

使用配置项

// 新建gunicorn_cinfig.py文件 // 指定端口 bind = '127.0.0.1:8000' // 线程数 workers = 4

加载配置文件,不配置supervisor后台运行启动

// 加载配置文件启动 gunicorn -c gunicorn_config.py 'applications:create_app()' > test.log 2>&1 &

进入虚拟环境

// # source venv/bin/activate # (Linux激活虚拟环境) // 启动supervisor ./bin/supervisord -c /etc/supervisor/supervisor.conf // 查看状态 ./bin/supervisorctl -c /etc/supervisor/supervisor.conf status // 停止全部进程 ./bin/supervisorctl -c /etc/supervisor/supervisor.conf shutdown // 启动指定服务 ./bin/supervisorctl -c /etc/supervisor/supervisor.conf start flask_app // flask_app服务的名称

supervisor常用命令

supervisord -c supervisor.conf // 通过配置文件启动supervisor supervisorctl -c supervisor.conf status // 查看supervisor的状态 supervisorctl -c supervisor.conf reload // 重新载入配置文件 supervisorctl -c supervisor.conf start [all]|[appname] // 启动指定/所有 supervisor管理的程序进程 supervisorctl -c supervisor.conf stop [all]|[appname] // 关闭指定/所有 supervisor管理的程序进程
文章评论
总共 0 条评论
这篇文章还没有收到评论,赶紧来抢沙发吧~
Copyright (C) 2023- 小祥驿站 保留所有权利 蜀ICP备 17034318号-2  公安备案号 50010302004554