Supervisor无法与Gunicorn和Flask配合使用

7

我正在尝试在Ubuntu 12.04系统中使用Supervisor运行Gunicorn。Gunicorn运行一个Flask应用程序(使用Flask的嵌入式服务器测试的简单REST Web服务)。我已经通过克隆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)

检查管理员日志:

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中。其他模块需要lib路径。我还编辑了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个回答

9

我看不到你在supervisor配置文件中设置环境:

[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

是的,我正在做那个,并且在你回复我的同时编辑我的问题...抱歉!我正在按照你说的做,请等一下! :) - Alberto Megía
它告诉我找不到“const”模块,但它在/usr/local/lib/project中,并已在Supervisor的配置文件中添加到环境中... - Alberto Megía
同样的问题:没有名为'const'的模块。但是它确实存在,并且已经被Python编译!(出现了pyc文件)。我认为这是Supervisor cd的问题,因为我只能通过自己设置PYTHONPATH来使用Gunicorn运行它... - Alberto Megía
2
我刚刚注意到gunicorn的--pythonpath可能实际上是有问题的。尝试仅设置--pythonpath /usr/local/lib/project - robertklep
1
设置 --pythonpath /path/to/project 解决了我的问题。谢谢! - Greg Wang
显示剩余2条评论

4

不需要传递--pythonpath参数。如果您使用virtuanenv,则可以添加gunicorn所在的位置。例如:

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

目录是指 Flask 代码的位置,例如:

directory=/home/virtualenv/myapp

记住用户是root!

user=root

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接