Supervisor与Gunicorn + Flask不兼容

7 投票
2 回答
18726 浏览
提问于 2025-04-17 18:24

我正在尝试在Ubuntu 12.04系统中通过Supervisor运行Gunicorn。Gunicorn是用来运行Flask应用的(这是一个简单的REST网络服务,已经用Flask自带的服务器测试过)。我通过克隆GIT仓库来安装Gunicorn,想避免使用'apt-get install',因为那样安装时会自动启动Gunicorn服务器。我不想让它运行,只想通过Supervisor来启动。

所以在安装后,如果我尝试:

cd /usr/local/bin
gunicorn my_app:app -c /path/to/gu_config_file

Gunicorn可以正常工作。然后我把它杀掉。注意配置文件没有扩展名,因为加上'.py'扩展名对我来说不管用。

接着我编辑了Supervisor的配置文件,像这样:

[program:gunicorn]
command=/usr/local/bin/gunicorn my_app:app -c /path/to/.gu_setup
directory=/usr/local/bin/
autostart=true
autorestart=true
redirect_stderr=True

然后在Supervisor中更新了更改:

supervisorctl reread
# gunicorn: changed
supervisorctl update
# gunicorn: stopped
# gunicorn: updated process group

它检测到文件的变化,并且对Gunicorn程序有效。好的,但当我尝试启动它时:

supervisorctl start gunicorn

出现了一个烦人的错误:

gunicorn: ERROR (abnormal termination)

查看Supervisor的日志:

2013-03-08 13:07:22,378 INFO spawned: 'gunicorn' with pid 3355
2013-03-08 13:07:22,916 INFO exited: gunicorn (exit status 3; not expected)
2013-03-08 13:07:23,918 INFO spawned: 'gunicorn' with pid 3361
2013-03-08 13:07:24,492 INFO exited: gunicorn (exit status 3; not expected)
2013-03-08 13:07:26,496 INFO spawned: 'gunicorn' with pid 3367
2013-03-08 13:07:27,078 INFO exited: gunicorn (exit status 3; not expected)
2013-03-08 13:07:30,085 INFO spawned: 'gunicorn' with pid 3373
2013-03-08 13:07:30,628 INFO exited: gunicorn (exit status 3; not expected)
2013-03-08 13:07:31,630 INFO gave up: gunicorn entered FATAL state, too many start retries too quickly

我现在不知道该怎么办……你能帮我吗?非常感谢!

编辑:抱歉,我忘了说我已经将PYTHONPATH变量导出为:

export PYTHONPATH=/usr/local/bin:/usr/local/lib/project

'my_app'在/usr/local/bin。这个库路径是为了其他模块需要的。

我还编辑了Supervisor的配置文件,以指明环境变量,像这样:

environment=PYTHONPATH=/usr/local/bin:/usr/local/lib/project/

但没有成功。

编辑2:正如@robertklep在他的评论中建议的,这是日志的输出:

Traceback (most recent call last):
  File "/tmp/gunicorn/gunicorn/arbiter.py", line 485, in spawn_worker
    worker.init_process()
  File "/tmp/gunicorn/gunicorn/workers/base.py", line 100, in init_process
    self.wsgi = self.app.wsgi()
  File "/tmp/gunicorn/gunicorn/app/base.py", line 103, in wsgi
    self.callable = self.load()
  File "/tmp/gunicorn/gunicorn/app/wsgiapp.py", line 25, in load
    return util.import_app(self.app_uri)
  File "/tmp/gunicorn/gunicorn/util.py", line 369, in import_app
    __import__(module)
  File "/usr/local/bin/my_app.py", line 4, in <module>
    import const
ImportError: No module named const
2013-03-08 13:29:35 [3670] [INFO] Worker exiting (pid: 3670)
2013-03-08 13:29:36 [3665] [INFO] Shutting down: Master
2013-03-08 13:29:36 [3665] [INFO] Reason: Worker failed to boot.

'const'模块在/usr/local/lib/project...

2 个回答

4

其实不需要传递 --pythonpath 这个参数。如果你在使用虚拟环境(virtualenv),你只需要添加 gunicorn 的位置就可以了。比如:

command=/home/virtualenv/bin/gunicorn application:app -c /home/virtualenv/deploy/gunicorn.conf.py

还有,目录就是你放 Flask 代码的地方,比如:

directory=/home/virtualenv/myapp

记得用户是 root 权限哦!

user=root
9

我没有看到你在你的管理配置文件中设置环境变量:

[program:gunicorn]
environment=PYTHONPATH=/usr/local/bin:/usr/local/lib/project
command=/usr/local/bin/gunicorn my_app:app -c /path/to/.gu_setup
...

如果这样不行,试着以调试模式启动gunicorn:

command=/usr/local/bin/gunicorn --debug --log-level debug my_app:app -c /path/to/.gu_setup

或者直接把路径传给gunicorn:

command=/usr/local/bin/gunicorn --pythonpath /usr/local/bin,/usr/local/lib/project my_app:app -c /path/to/.gu_setup

补充:gunicorn的 --pythonpath 有问题,你只能传一个目录:

command=/usr/local/bin/gunicorn --pythonpath /usr/local/lib/project my_app:app -c /path/to/.gu_setup

撰写回答