折腾:
【已解决】Flask本地和线上用gunicorn和supervisor部署时如何传入环境变量
期间,已经试了很多次的:
1 | supervisord -c conf /supervisor/supervisord_local .conf |
但是发现:
有时候可以正常启动,有时候不行。
其中不行的时候,往往是:
supervisord -c conf/supervisor/supervisord_local.conf
终端不输出任何东西。
正常的时候,一般输出是:
1 2 | ➜ xxxRobotDemoServer git:(master) ✗ supervisord -c conf /supervisor/supervisord_local .conf Unlinking stale socket /var/run/supervisor .sock |
但也不一定:
比如刚刚就发现:
1 2 | ➜ xxxRobotDemoServer git:(master) ✗ supervisorctl status robotDemo EXITED Aug 22 03:47 PM |
实际上还是退出了。
去看log的话,此处是:
logs/gunicorn_error.log
1 2 3 | [2018-08-22 15:47:51 +0800] [60354] [ERROR] Connection in use: ( '0.0.0.0' , 32851) [2018-08-22 15:47:51 +0800] [60354] [ERROR] Retrying in 1 second. [2018-08-22 15:47:52 +0800] [60354] [ERROR] Can 't connect to (' 0.0.0.0', 32851) |
logs/supervisord.log
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 2018 - 08 - 22 15 : 47 : 45 , 431 INFO RPC interface 'supervisor' initialized 2018 - 08 - 22 15 : 47 : 45 , 431 CRIT Server 'inet_http_server' running without any HTTP authentication checking 2018 - 08 - 22 15 : 47 : 45 , 736 INFO RPC interface 'supervisor' initialized 2018 - 08 - 22 15 : 47 : 45 , 736 CRIT Server 'unix_http_server' running without any HTTP authentication checking 2018 - 08 - 22 15 : 47 : 45 , 738 INFO daemonizing the supervisord process 2018 - 08 - 22 15 : 47 : 45 , 740 INFO supervisord started with pid 60345 2018 - 08 - 22 15 : 47 : 46 , 746 INFO spawned: 'robotDemo' with pid 60354 2018 - 08 - 22 15 : 47 : 47 , 751 INFO success: robotDemo entered RUNNING state, process has stayed up for > than 0 seconds (startsecs) 2018 - 08 - 22 15 : 47 : 52 , 275 DEBG 'robotDemo' stdout output: curPath = / Users / crifan / dev / dev_root / company / xxx / projects / robotDemo / server / xxxRobotDemoServer / conf / gunicorn confPath = / Users / crifan / dev / dev_root / company / xxx / projects / robotDemo / server / xxxRobotDemoServer / conf / gunicorn / .. Before load .env: DEBUG = False , MONGODB_HOST = localhost, FILE_URL_HOST = 127.0 . 0.1 cur_flask_environ = production FLASK_ENV = production cur_dir = / Users / crifan / dev / dev_root / company / xxx / projects / robotDemo / server / xxxRobotDemoServer / conf / app env_folder = production dotenv_path = / Users / crifan / dev / dev_root / company / xxx / projects / robotDemo / server / xxxRobotDemoServer / conf / app / production / .env dotenv_load_ok = True After load .env: DEBUG = False , MONGODB_HOST = localhost, FILE_URL_HOST = xxx settings = <module 'conf.app.settings' from '/Users/crifan/dev/dev_root/company/xxx/projects/robotDemo/server/xxxRobotDemoServer/conf/app/settings.py' > appRootPath = / Users / crifan / dev / dev_root / company / xxx / projects / robotDemo / server / xxxRobotDemoServer / conf / gunicorn / .. / .. flaskHost = 0.0 . 0.0 , flaskPort = 32851 2018 - 08 - 22 15 : 47 : 52 , 341 DEBG fd 13 closed, stopped monitoring <POutputDispatcher at 4396817800 for <Subprocess at 4396120544 with name robotDemo in state RUNNING> (stderr)> 2018 - 08 - 22 15 : 47 : 52 , 341 DEBG fd 11 closed, stopped monitoring <POutputDispatcher at 4396817440 for <Subprocess at 4396120544 with name robotDemo in state RUNNING> (stdout)> 2018 - 08 - 22 15 : 47 : 52 , 342 INFO exited: robotDemo (exit status 1 ; not expected) 2018 - 08 - 22 15 : 47 : 52 , 342 DEBG received SIGCHLD indicating a child quit |
此处不正常,则:

没有robotDemo.log
-》正常启动的话,是有的。
需要搞清楚此处到底为何,有时候可以运行,有时候又不行。
后来也去试了试:
1 2 3 4 5 6 7 | [program:robotDemo] ;command = / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / gunicorn - c gunicorn_config.py app:app command = / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / gunicorn - c conf / gunicorn / gunicorn_config.py app:app environment = FLASK_ENV = "production" ... ;autorestart = true autorestart = false |
避免每次挂了,就一直重启->导致Mac都卡了。
注意到:
1 | [ERROR] Connection in use: ( '0.0.0.0' , 32851) |
难道是:
mac本地只能运行单个gunicorn?的单个线程?
flask gunicorn [ERROR] Connection in use 0.0.0.0
flask mac gunicorn [ERROR] Connection in use 0.0.0.0
去看之前自己的贴子:
先再去确认进程:
1 2 | ➜ xxxRobotDemoServer git:(master) ✗ ps aux | grep 32581 crifan 60599 0.0 0.0 4276968 892 s007 S + 4 : 00PM 0 : 00.00 grep - - color = auto - - exclude - dir = .bzr - - exclude - dir = CVS - - exclude - dir = .git - - exclude - dir = .hg - - exclude - dir = .svn 32581 |
没有。
1 2 3 4 | ➜ xxxRobotDemoServer git:(master) ✗ netstat -tulpn netstat : n: unknown or uninstrumented protocol ➜ xxxRobotDemoServer git:(master) ✗ lsof -i:32581 ➜ xxxRobotDemoServer git:(master) ✗ |
还是没有。
去搜gunicorn,结果还真的发现很多:
1 2 3 4 5 6 7 8 9 10 11 12 13 | ➜ xxxRobotDemoServer git:(master) ✗ ps - ax | grep gunicorn 57904 ?? 0 : 01.55 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / python3. 6 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / gunicorn - c conf / gunicorn / gunicorn_config.py app:app 59863 ?? 0 : 13.08 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / python3. 6 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / gunicorn - c conf / gunicorn / gunicorn_config.py app:app 59864 ?? 0 : 13.33 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / python3. 6 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / gunicorn - c conf / gunicorn / gunicorn_config.py app:app 59865 ?? 0 : 13.26 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / python3. 6 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / gunicorn - c conf / gunicorn / gunicorn_config.py app:app 59866 ?? 0 : 13.59 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / python3. 6 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / gunicorn - c conf / gunicorn / gunicorn_config.py app:app 59867 ?? 0 : 13.35 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / python3. 6 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / gunicorn - c conf / gunicorn / gunicorn_config.py app:app 59868 ?? 0 : 13.35 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / python3. 6 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / gunicorn - c conf / gunicorn / gunicorn_config.py app:app 59869 ?? 0 : 13.24 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / python3. 6 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / gunicorn - c conf / gunicorn / gunicorn_config.py app:app 59870 ?? 0 : 13.50 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / python3. 6 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / gunicorn - c conf / gunicorn / gunicorn_config.py app:app 59871 ?? 0 : 13.47 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / python3. 6 / Users / crifan / .local / share / virtualenvs / xxxRobotDemoServer - SCpLPEyZ / bin / gunicorn - c conf / gunicorn / gunicorn_config.py app:app 60690 ttys007 0 : 00.00 grep - - color = auto - - exclude - dir = .bzr - - exclude - dir = CVS - - exclude - dir = .git - - exclude - dir = .hg - - exclude - dir = .svn gunicorn ➜ xxxRobotDemoServer git:(master) ✗ |
彻底杀掉:
1 2 3 4 5 6 7 8 9 | ➜ xxxRobotDemoServer git:(master) ✗ kill - 9 57904 59863 59864 59865 59866 ➜ xxxRobotDemoServer git:(master) ✗ kill - 9 59867 59868 59869 59870 59871 kill: kill 59867 failed: no such process kill: kill 59868 failed: no such process kill: kill 59869 failed: no such process kill: kill 59870 failed: no such process kill: kill 59871 failed: no such process 60759 ttys007 0 : 00.00 grep - - color = auto - - exclude - dir = .bzr - - exclude - dir = CVS - - exclude - dir = .git - - exclude - dir = .hg - - exclude - dir = .svn gunicorn |
再彻底保证
supervisor和gunicorn都没有进程了:
1 2 3 4 5 6 7 8 9 10 11 12 13 | ➜ xxxRobotDemoServer git:(master) ✗ ps - ax | grep gunicorn 60759 ttys007 0 : 00.00 grep - - color = auto - - exclude - dir = .bzr - - exclude - dir = CVS - - exclude - dir = .git - - exclude - dir = .hg - - exclude - dir = .svn gunicorn ➜ xxxRobotDemoServer git:(master) ✗ ps aux | grep gunicorn crifan 60773 0.0 0.0 4276968 896 s007 S + 4 : 04PM 0 : 00.00 grep - - color = auto - - exclude - dir = .bzr - - exclude - dir = CVS - - exclude - dir = .git - - exclude - dir = .hg - - exclude - dir = .svn gunicorn ➜ xxxRobotDemoServer git:(master) ✗ ps aux | grep supervisor crifan 60783 0.0 0.0 4286184 924 s007 S + 4 : 04PM 0 : 00.00 grep - - color = auto - - exclude - dir = .bzr - - exclude - dir = CVS - - exclude - dir = .git - - exclude - dir = .hg - - exclude - dir = .svn supervisor crifan 60541 0.0 0.0 4309536 1576 ?? Ss 3 : 57PM 0 : 00.11 / usr / bin / python / Users / crifan / Library / Python / 2.7 / bin / supervisord - c conf / supervisor / supervisord_local.conf ➜ xxxRobotDemoServer git:(master) ✗ kill - 9 60541 ➜ xxxRobotDemoServer git:(master) ✗ ps aux | grep supervisor crifan 60804 0.0 0.0 4276968 648 s007 R + 4 : 04PM 0 : 00.00 grep - - color = auto - - exclude - dir = .bzr - - exclude - dir = CVS - - exclude - dir = .git - - exclude - dir = .hg - - exclude - dir = .svn supervisor ➜ xxxRobotDemoServer git:(master) ✗ ps aux | grep gunicorn crifan 60814 0.0 0.0 4275944 800 s007 R + 4 : 04PM 0 : 00.00 grep - - color = auto - - exclude - dir = .bzr - - exclude - dir = CVS - - exclude - dir = .git - - exclude - dir = .hg - - exclude - dir = .svn gunicorn ➜ xxxRobotDemoServer git:(master) ✗ |
然后再去运行:
终于可以正常启动了:
1 2 3 4 | ➜ xxxRobotDemoServer git:(master) ✗ supervisord -c conf /supervisor/supervisord_local .conf Unlinking stale socket /var/run/supervisor .sock ➜ xxxRobotDemoServer git:(master) ✗ supervisorctl status robotDemo RUNNING pid 60839, uptime 0:00:06 |
【总结】
此处Mac本地用gunicorn和supervisor去部署和运行Flask的app时:
1 | supervisord -c conf /supervisor/supervisord_local .conf |
有时候可以正常运行,有时候又不行。
最后发现是:
虽然通过:
1 2 | ps aux | grep 32851 |
去排除了还有在运行的(gunicorn的)端口
以及:
1 | ps aux | grep supervisor |
和supervisor了,但是实际上通过:
1 | ps -ax | grep gunicorn |
发现还是有正在运行的gunicorn的进程的
所以,此处对于gunicorn的log提示的错误:
[ERROR] Connection in use: (‘0.0.0.0’, 32851)
要找到全部的,当前还在运行的进程:
1 2 3 4 | ps aux | grep 32851 ps aux | grep gunicorn |
以及supervisor:
1 2 | ps aux | grep supervisor |
看到进程号后,再去完全干掉这些进程:
1 | kill -9 xxx |
然后再去重新启动supervisord:
1 | supervisord -c conf /supervisor/supervisord_local .conf |
就可以了正常接着用supervisorctl去管理了:
1 2 3 | supervisorctl status supervisorctl stop all supervisorctl reload |
等等。