最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Mac本地用supervisor去管理gunicorn的Python3的Flask

Flask crifan 4277浏览 0评论

折腾:

【已解决】把Python3的pipenv的Flask部署到CentOS服务器上

期间,已经本地用gunicorn去运行了flask的app。

接着需要去用supervisor去管理进程。

刚参考:

gunicorn 部署 flask 应用 – CSDN博客

发现:

“之前使用python3的使用,发现 supervisor是不支持。所以就不打算用supervisor了。”

去搜索看

flask supervisor

flask supervisor python3

supervisor python3

使用supervisor支持Python3程序 | 不正经数据科学家

supervisor · PyPI

“Supervisor is a client/server system that allows its users to control a number of processes on UNIX-like operating systems.

Supervisor has been tested and is known to run on Linux (Ubuntu), Mac OS X (10.4, 10.5, 10.6), and Solaris (10 for Intel) and FreeBSD 6.1. It will likely work fine on most UNIX systems.

Supervisor will not run at all under any version of Windows.

Supervisor is known to work with Python 2.4 or later but will not work under any version of Python 3.”

不支持Windows

不支持Python 3,只支持  Python 2.4+

关于python3.x上supervisor安装问题? – 知乎

Supervisor/supervisor: Supervisor process control system for UNIX

“Supervisor 4.0 (unreleased) is designed to work under Python 3 version 3.4 or later and on Python 2 version 2.7.”

但是Latest的最新版本:

supervisor 3.3.4

还是不支持Python3

supervisor 在python3下的简易解决方案 – CSDN博客

此处pipenv中python3去安装supervisor果然找不到:

<code>➜  robotDemo which python
/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/bin/python
➜  robotDemo python --version
Python 3.6.4
➜  robotDemo pipenv install surpervisor
Installing surpervisor…
Collecting surpervisor

Error:  An error occurred while installing surpervisor!
  Could not find a version that satisfies the requirement surpervisor (from versions: )
No matching distribution found for surpervisor
</code>

继续用pip试试,也不行:

<code>➜  robotDemo which pip
pip: aliased to noglob pip
➜  robotDemo which pip3
/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/bin/pip3
➜  robotDemo pip3 --version
pip 10.0.0 from /Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/lib/python3.6/site-packages/pip (python 3.6)
➜  robotDemo pip3 install supervisor
Collecting supervisor
  Downloading https://files.pythonhosted.org/packages/44/60/698e54b4a4a9b956b2d709b4b7b676119c833d811d53ee2500f1b5e96dc3/supervisor-3.3.4.tar.gz (419kB)
    100% |████████████████████████████████| 419kB 772kB/s
    Complete output from command python setup.py egg_info:
    Supervisor requires Python 2.4 or later but does not work on any version of Python 3.  You are using version 3.6.4 (default, Mar 22 2018, 13:54:22)
    [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)].  Please install using a supported version.

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/46/2hjxz38n22n3ypp_5f6_p__00000gn/T/pip-install-3zz2e51v/supervisor/
</code>

然后虽然已退出pipenv了,而且本地Mac系统中python是2的,但是由于之前pip被强制重新安装未为pip3的了,导致安装也失败:

<code>➜  robotDemo exit
➜  robotDemo which python
/usr/bin/python
➜  robotDemo python --version
Python 2.7.10
➜  robotDemo pip install supervisor
Collecting supervisor
  Using cached https://files.pythonhosted.org/packages/44/60/698e54b4a4a9b956b2d709b4b7b676119c833d811d53ee2500f1b5e96dc3/supervisor-3.3.4.tar.gz
    Complete output from command python setup.py egg_info:
    Supervisor requires Python 2.4 or later but does not work on any version of Python 3.  You are using version 3.6.4 (default, Mar 22 2018, 13:54:22)
    [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)].  Please install using a supported version.

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/46/2hjxz38n22n3ypp_5f6_p__00000gn/T/pip-install-3woroxix/supervisor/
➜  robotDemo which pip
pip: aliased to noglob pip
➜  robotDemo where pip
pip: aliased to noglob pip
/usr/local/bin/pip
➜  robotDemo pip --version
pip 10.0.0 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)
</code>

所以此处好像需要去:

【已解决】Mac中把Python3的pip重新换成Python2的pip

然后再去用python2的pip去安装supervisor

<code>➜  robotDemo pip install supervisor
Collecting supervisor
  Using cached https://files.pythonhosted.org/packages/44/60/698e54b4a4a9b956b2d709b4b7b676119c833d811d53ee2500f1b5e96dc3/supervisor-3.3.4.tar.gz
Collecting meld3&gt;=0.6.5 (from supervisor)
  Downloading https://files.pythonhosted.org/packages/b6/ae/e6d731e4b9661642c1b20591d8054855bb5b8281cbfa18f561c2edd783f7/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: /Users/crifan/Library/Caches/pip/wheels/05/9f/2f/49c4d02a06ef353b5647ddc018f971a44809fe65c772d95511
Successfully built supervisor
pipenv 11.10.0 requires certifi, which is not installed.
pipenv 11.10.0 requires requests[security], which is not installed.
pipenv 11.10.0 requires virtualenv, which is not installed.
matplotlib 1.3.1 requires nose, which is not installed.
matplotlib 1.3.1 requires tornado, which is not installed.
Installing collected packages: meld3, supervisor
Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/meld3-1.0.2.dist-info'
Consider using the `--user` option or check the permissions.

➜  robotDemo ll /Library/Python/2.7/site-packagesme/
➜  robotDemo ll /Library/Python/2.7/site-packages/meld3-1.0.2.dist-info
ls: /Library/Python/2.7/site-packages/meld3-1.0.2.dist-info: No such file or directory
➜  robotDemo
➜  robotDemo ls -lha /Library/Python/2.7/site-packages
total 0
drwxr-xr-x  4 root  wheel   128B 10 26 00:34 .
drwxr-xr-x  3 root  wheel    96B  9 12  2017 ..
-rwxr-xr-x  1 root  wheel   157B  7 16  2017 Extras.pth
-rw-r--r--  1 root  wheel   119B  7 16  2017 README
➜  robotDemo
➜  robotDemo
➜  robotDemo pip install supervisor --user
Collecting supervisor
Collecting meld3&gt;=0.6.5 (from supervisor)
  Using cached https://files.pythonhosted.org/packages/b6/ae/e6d731e4b9661642c1b20591d8054855bb5b8281cbfa18f561c2edd783f7/meld3-1.0.2-py2.py3-none-any.whl
pipenv 11.10.0 requires certifi, which is not installed.
pipenv 11.10.0 requires requests[security], which is not installed.
pipenv 11.10.0 requires virtualenv, which is not installed.
matplotlib 1.3.1 requires nose, which is not installed.
matplotlib 1.3.1 requires tornado, which is not installed.
Installing collected packages: meld3, supervisor
  The scripts echo_supervisord_conf, pidproxy, supervisorctl and supervisord are installed in '/Users/crifan/Library/Python/2.7/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed meld3-1.0.2 supervisor-3.3.4
➜  robotDemo
</code>

搜:

echo_supervisord_conf

另外看到:

<code>➜  robotDemo ll /Users/crifan/Library/Python/2.7/bin
total 80
-rwxr-xr-x  1 crifan  staff   253B  4 17 10:19 easy_install
-rwxr-xr-x  1 crifan  staff   253B  4 17 10:19 easy_install-2.7
-rwxr-xr-x  1 crifan  staff   221B  4 20 16:13 echo_supervisord_conf
-rwxr-xr-x  1 crifan  staff   221B  4 20 16:13 pidproxy
-rwxr-xr-x  1 crifan  staff   215B  4 20 15:50 pip
-rwxr-xr-x  1 crifan  staff   215B  4 20 15:50 pip2
-rwxr-xr-x  1 crifan  staff   215B  4 20 15:50 pip2.7
-rwxr-xr-x  1 crifan  staff   226B  4 20 16:13 supervisorctl
-rwxr-xr-x  1 crifan  staff   224B  4 20 16:13 supervisord
-rwxr-xr-x  1 crifan  staff   212B  4 20 15:26 wheel
</code>

所以还是要去把:

/Users/crifan/Library/Python/2.7/bin

加到PATH中。

<code>➜  robotDemo ll /Users/crifan/Library/Python/2.7/bin
total 80
-rwxr-xr-x  1 crifan  staff   253B  4 17 10:19 easy_install
-rwxr-xr-x  1 crifan  staff   253B  4 17 10:19 easy_install-2.7
-rwxr-xr-x  1 crifan  staff   221B  4 20 16:13 echo_supervisord_conf
-rwxr-xr-x  1 crifan  staff   221B  4 20 16:13 pidproxy
-rwxr-xr-x  1 crifan  staff   215B  4 20 15:50 pip
-rwxr-xr-x  1 crifan  staff   215B  4 20 15:50 pip2
-rwxr-xr-x  1 crifan  staff   215B  4 20 15:50 pip2.7
-rwxr-xr-x  1 crifan  staff   226B  4 20 16:13 supervisorctl
-rwxr-xr-x  1 crifan  staff   224B  4 20 16:13 supervisord
-rwxr-xr-x  1 crifan  staff   212B  4 20 15:26 wheel
➜  robotDemo ll /Users/crifan/Library/Python/3.6/bin
total 40
-rwxr-xr-x  1 crifan  staff   242B  4 17 14:35 pewtwo
-rwxr-xr-x  1 crifan  staff   226B  4 17 14:35 pipenv
-rwxr-xr-x  1 crifan  staff   237B  4 17 14:35 pipenv-resolver
-rwxr-xr-x  1 crifan  staff   232B  4 17 11:06 virtualenv
-rwxr-xr-x  1 crifan  staff   237B  4 17 11:06 virtualenv-clone
➜  robotDemo vi ~/.bashrc
➜  robotDemo cat ~/.bashrc

export NVM_DIR="/Users/crifan/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] &amp;&amp; . "$NVM_DIR/nvm.sh"  # This loads nvm

export PATH="/Users/crifan/Library/Python/2.7/bin:/Users/crifan/Library/Python/3.6/bin:$PATH"

➜  robotDemo source ~/.bashrc
➜  robotDemo echo $PATH
/Users/crifan/Library/Python/2.7/bin:/Users/crifan/Library/Python/3.6/bin:/Users/crifan/.nvm/versions/node/v5.4.1/bin:/Users/crifan/Library/Python/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
➜  robotDemo which python
/usr/bin/python
</code>

使用supervisor支持Python3程序 | 不正经数据科学家

然后去试试:

<code>➜  robotDemo echo_supervisord_conf &gt; /etc/supervisor/supervisord.conf
zsh: no such file or directory: /etc/supervisor/supervisord.conf
➜  robotDemo echo_supervisord_conf &gt; /etc/supervisord.conf
zsh: permission denied: /etc/supervisord.conf
</code>

参考:

Installing — Supervisor 3.3.4 documentation

去保存到项目本地:

<code>➜  robotDemo echo_supervisord_conf &gt; supervisord.conf
</code>

看看内容:

然后去配置

Running Supervisor — Supervisor 3.3.4 documentation

Python Web 部署: 使用 flask + gunicorn + supervisor… – 简书

然后再去命令行运行,竟然没有任何输出:

<code>➜  robotDemo supervisord -c supervisord.conf
</code>

但却发现奇怪的事情:

【未解决】supervisord的普通stdout的log却输出到表示错误的stderr的log中

不过,接口倒是正常工作了好像:

Chrome浏览器去直接访问了,有返回json:

对应的supervisord的log是:

而gunicorn的log是:

再去试试正常访问:

log:

logs/gunicorn_access.log:

logs/gunicorn_error.log:

没有变化 -》 只是在启动时才有输出log

而此处的,之所以可以让

本身不支持Python3的,当然用python2(的pip安装)的supervisor,能够正常运行Python3的pipenv虚拟环境中的gunicorn和Flask的关键一步是:

参考了

Ubuntu部署python3-flask-nginx-uwsgi-supervisor完美-joker-51CTO博客

<code>[program:python]
command=/root/.virtualenvs/py3-zqcms/bin/uwsgi --uid www --gid www --ini /www/wwwroot/uwsgi.ini
</code>

和:

flask + nginx + uwsgi + python3 + supervisor做flask的nginx代理 | 禹都一只猫

<code>[program:flask]
command=/root/.virtualenvs/py3_flask/bin/uwsgi /root/flask/hello.ini
</code>

中的command中,要运行的uwsgi是虚拟环境中的程序

-》uwsgi换成此处我用的gunicorn(或者是虚拟环境中的python3),才能正常运行虚拟环境中的Python3的程序(Flask)

然后去试试supervisorctl命令行管理app

<code>➜  robotDemo supervisorctl status all
myapp                            RUNNING   pid 10323, uptime 0:36:26
➜  robotDemo supervisorctl stop all
myapp: stopped
➜  robotDemo supervisorctl status all
myapp                            STOPPED   Apr 20 05:50 PM
➜  robotDemo supervisorctl start  all
myapp: started
➜  robotDemo supervisorctl status all
myapp                            RUNNING   pid 10794, uptime 0:00:07
➜  robotDemo supervisorctl stop all
myapp: stopped
➜  robotDemo supervisorctl start  all
myapp: started
➜  robotDemo supervisorctl stop all
myapp: stopped
➜  robotDemo supervisorctl start  all
myapp: started
➜  robotDemo supervisorctl stop all
myapp: stopped
</code>

在把默认的app的名字:myapp改为:robotDemo

后,再去试试reload:

<code>➜  robotDemo supervisorctl reload all
Error: reload accepts no arguments
reload         Restart the remote supervisord.
➜  robotDemo supervisorctl reload
Restarted supervisord
➜  robotDemo supervisorctl stop all
robotDemo: stopped
➜  robotDemo supervisorctl start  all
robotDemo: started
➜  robotDemo supervisorctl status all
robotDemo                        RUNNING   pid 10912, uptime 0:00:00
➜  robotDemo
</code>

抽空再去试试:

用supervidord管理运行的gunicorn的reload是否有效:故意改动代码,看看能否自动重启gunicorn

-》是可以的

-〉改了代码后,新请求的接口就是最新的代码返回的结果了:

且之前的gunicorn的reload也检测到文件改动了:

然后以及:

在request的代码中,写错语法,然后去测试接口,导致server 500内部出错 -》 看看 supervisor(或gunicorn)是否可以自动 autorestart

故意改错代码:

然后去访问对应接口,使得出现:

{“message”: “Internal Server Error”}

内部log也显示代码出错了:

然后是:

内部应该是重启了?

总之是可以正常访问其他接口的:

【总结】

此处用supervisor去管理gunicorn的话,本来很简单。

但是考虑到此处是Python3的Flask,所以要有些特殊的注意事项:

supervisord.conf

配置文件中的:

command中的gunicorn,要是虚拟环境中的(带绝对路径)gunicorn

完整的supervisord.conf配置如下:

<code>[unix_http_server]
;file=/tmp/supervisor.sock   ; the path to the socket file
file=/var/run/supervisor.sock   ; the path to the socket file
chmod=0766                 ; socket file mode (default 0700)
;chown=nobody:nogroup       ; socket file uid:gid owner
;username=user              ; default is no username (open server)
;password=123               ; default is no password (open server)

;[inet_http_server]         ; inet (TCP) server disabled by default
;port=127.0.0.1:9001        ; ip_address:port specifier, *:port for all iface
;username=user              ; default is no username (open server)
;password=123               ; default is no password (open server)

[supervisord]
;logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
;logfile=/var/run/supervisord.log ; main log file; default $CWD/supervisord.log
logfile=/Users/crifan/dev/dev_root/company/naturling/projects/robotDemo/logs/supervisord.log
;logfile_maxbytes=50MB        ; max main logfile bytes b4 rotation; default 50MB
logfile_maxbytes=2MB        ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10           ; # of main logfile backups; 0 means none, default 10
;loglevel=info               ; log level; default info; others: debug,warn,trace
loglevel=debug               ; log level; default info; others: debug,warn,trace
;pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
pidfile=/var/run/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false               ; start in foreground if true; default false
minfds=1024                  ; min. avail startup file descriptors; default 1024
minprocs=200                 ; min. avail process descriptors;default 200
;umask=022                   ; process file creation umask; default 022
;user=chrism                 ; default is current user, required if root
;identifier=supervisor       ; supervisord identifier, default is 'supervisor'
;directory=/tmp              ; default is not to cd during start
;nocleanup=true              ; don't clean up tempfiles at start; default false
;childlogdir=/tmp            ; 'AUTO' child log dir, default $TEMP
;environment=KEY="value"     ; key value pairs to add to environment
;strip_ansi=false            ; strip ansi escape codes in logs; def. false


[supervisorctl]
;serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris              ; should be same as in [*_http_server] if set
;password=123                ; should be same as in [*_http_server] if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available

[program:robotDemo]
command=/Users/crifan/.local/share/virtualenvs/robotDemo-HXjMJQEQ/bin/gunicorn -c gunicorn_config.py app:app
;user=root
directory=/Users/crifan/dev/dev_root/company/naturling/projects/robotDemo
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true

stdout_logfile=/Users/crifan/dev/dev_root/company/naturling/projects/robotDemo/logs/supervisord_out.log
stdout_logfile_maxbytes=2MB
stdout_logfile_backups=10

stderr_logfile=/Users/crifan/dev/dev_root/company/naturling/projects/robotDemo/logs/supervisord_err.log
stderr_logfile_maxbytes=2MB
stderr_logfile_backups=10

</code>

注意:

在你的:

[program:robotDemo]

之前,默认配置中已有了:

[supervisord]

[supervisorctl]

不要自己再(像我之前不知道)在配置文件最后单独去加了:

[supervisord]

[supervisorctl]

了。

应该基于上面的配置去修改才对。

之后即可去运行和管理了:

<code>➜  robotDemo supervisorctl reoad
➜  robotDemo supervisorctl status all
➜  robotDemo supervisorctl stop all
➜  robotDemo supervisorctl start all
➜  robotDemo supervisorctl restart all
</code>

转载请注明:在路上 » 【已解决】Mac本地用supervisor去管理gunicorn的Python3的Flask

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
81 queries in 0.192 seconds, using 22.21MB memory