目前,对于Flask项目,每次重新启动时,都很麻烦,需要:
手动去kill掉对应的进程,且要等一段时间:
等之前的4个workker都退出了:
(RunningFast) ➜ RunningFast (RunningFast) ➜ RunningFast netstat -tulpn | grep 21084 tcp 0 0 0.0.0.0:21084 0.0.0.0:* LISTEN 1312/python (RunningFast) ➜ RunningFast kill -9 1312 [1] + 1312 killed gunicorn -w 4 -b 0.0.0.0:21084 run:app (RunningFast) ➜ RunningFast (RunningFast) ➜ RunningFast (RunningFast) ➜ RunningFast (RunningFast) ➜ RunningFast (RunningFast) ➜ RunningFast (RunningFast) ➜ RunningFast (RunningFast) ➜ RunningFast (RunningFast) ➜ RunningFast (RunningFast) ➜ RunningFast (RunningFast) ➜ RunningFast (RunningFast) ➜ RunningFast (RunningFast) ➜ RunningFast (RunningFast) ➜ RunningFast (RunningFast) ➜ RunningFast (RunningFast) ➜ RunningFast (RunningFast) ➜ RunningFast (RunningFast) ➜ RunningFast [2016-09-29 20:25:10 +0000] [1322] [INFO] Parent changed, shutting down: <Worker 1322> [2016-09-29 20:25:10 +0000] [1322] [INFO] Worker exiting (pid: 1322) [2016-09-29 20:25:10 +0000] [1323] [INFO] Parent changed, shutting down: <Worker 1323> [2016-09-29 20:25:10 +0000] [1323] [INFO] Worker exiting (pid: 1323) [2016-09-29 20:25:10 +0000] [1328] [INFO] Parent changed, shutting down: <Worker 1328> [2016-09-29 20:25:10 +0000] [1328] [INFO] Worker exiting (pid: 1328) [2016-09-29 20:25:10 +0000] [1329] [INFO] Parent changed, shutting down: <Worker 1329> [2016-09-29 20:25:10 +0000] [1329] [INFO] Worker exiting (pid: 1329) (RunningFast) ➜ RunningFast (RunningFast) ➜ RunningFast |
才能重启Flask:
gunicorn -w 4 -b 0.0.0.0:21084 run:app & |
现在希望:
把之前看到的,好像叫做supervisor的,用于任务管理的东西加进来:
可以使得每次重新启动Flask不再这么麻烦,只需要运行一个命令就好,比如restart之类的。
flask gunicorn task 管理
用gunicorn和gevent提高python web框架的性能 | 峰云就她了
python web 部署:nginx + gunicorn + supervisor + flask 部署笔记 – Python – 伯乐在线
(RunningFast) ➜ RunningFast pip install supervisor Collecting supervisor Downloading supervisor-3.3.1.tar.gz (415kB) 100% |████████████████████████████████| 419kB 31kB/s Collecting meld3>=0.6.5 (from supervisor) Downloading meld3-1.0.2-py2.py3-none-any.whl Building wheels for collected packages: supervisor Running setup.py bdist_wheel for supervisor … done Stored in directory: /root/.cache/pip/wheels/13/ea/25/c236d39561d2be9c427655e5d48d0bd2b75dc8e7e8b51d029d Successfully built supervisor Installing collected packages: meld3, supervisor Successfully installed meld3-1.0.2 supervisor-3.3.1 |
【已解决】安装了supervisor后找不到/etc/supervisord.conf和supervisor
然后自己去使用配置去折腾,结果运行没反应的感觉:
【已解决】supervisor调用gunicorn去运行Flask的app没反应
然后此处即可通过:
[program:runningfast] directory=/root/RunningFast command=/root/Envs/RunningFast/bin/gunicorn -w 4 -b 0.0.0.0:21084 run:app & startsecs=0 stopwaitsecs=0 autostart=false autorestart=false stdout_logfile=/root/RunningFast/logs/gunicorn.log stderr_logfile=/root/RunningFast/logs/gunicorn.err |
的配置,以及:
1.去启动supervisord:
supervisord -c supervisor.conf |
2.(在配置文件改动的情况下)重新加载
supervisorctl -c supervisor.conf reload |
3.去启动和停止自己的Flask的app:
supervisorctl -c supervisor.conf start runningfast supervisorctl -c supervisor.conf stop runningfast supervisorctl -c supervisor.conf restart runningfast |
之后每次即可运行上述两个命令去启动和停止即可。
另外:
<code>supervisorctl -c supervisor.conf status </code>
可以查看状态。