6

我正在尝试在 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

独角兽的作品。然后我杀了它。注意没有扩展名的配置文件,因为带有“.py”扩展名的我对我不起作用。所以我编辑主管的配置文件,如:

[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 路径。我还编辑了主管配置文件以指示环境变量,例如:

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...

4

2 回答 2

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
于 2013-03-08T12:24:40.303 回答
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
于 2013-09-20T12:23:16.803 回答