Gunicorn在Heroku上引发错误。

3
我刚将我的应用程序首次推送到Heroku,但每当我访问页面时都会出现“应用程序错误”。我不熟悉Heroku,并且似乎在帮助/开发中心链接中找不到任何参考资料。
查看Heroku日志,我得到了以下内容:
C:\Users\XXX\Desktop\xxxxx.folder>heroku logs
2013-10-14T02:32:18.578976+00:00 heroku[api]: Enable Logplex by EMAIL@email.com
2013-10-14T02:32:18.597277+00:00 heroku[api]: Release v2 created by EMAIL@email.com
2013-10-14T02:32:46+00:00 heroku[slug-compiler]: Slug compilation started
2013-10-14T02:33:10.309086+00:00 heroku[api]: Scale to web=1 by EMAIL@email.com
2013-10-14T02:33:10.356490+00:00 heroku[api]: Deploy 9edccd8 by EMAIL@email.com
2013-10-14T02:33:10.375197+00:00 heroku[api]: Release v3 created by EMAIL@email.com
2013-10-14T02:33:10+00:00 heroku[slug-compiler]: Slug compilation finished
2013-10-14T02:33:14.369318+00:00 heroku[web.1]: Starting process with command `gunicorn xxxxx:app`
2013-10-14T02:33:15.336306+00:00 app[web.1]: 2013-10-14 02:33:15 [2] [INFO] Starting gunicorn 18.0
2013-10-14T02:33:15.337257+00:00 app[web.1]: 2013-10-14 02:33:15 [2] [INFO] Listening at: http://0.0.0.0:36245 (2)
2013-10-14T02:33:15.337821+00:00 app[web.1]: 2013-10-14 02:33:15 [2] [INFO] Using worker: sync
2013-10-14T02:33:15.354973+00:00 app[web.1]: 2013-10-14 02:33:15 [7] [INFO] Booting worker with pid: 7
2013-10-14T02:33:15.360482+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 106, in init_process
2013-10-14T02:33:15.360482+00:00 app[web.1]:     self.callable = self.load()
2013-10-14T02:33:15.360482+00:00 app[web.1]: Traceback (most recent call last):
2013-10-14T02:33:15.360482+00:00 app[web.1]: 2013-10-14 02:33:15 [7] [ERROR] Exception in worker process:
2013-10-14T02:33:15.360482+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495, in spawn_worker
2013-10-14T02:33:15.360482+00:00 app[web.1]:     worker.init_process()
2013-10-14T02:33:15.360482+00:00 app[web.1]:     self.wsgi = self.app.wsgi()
2013-10-14T02:33:15.360482+00:00 app[web.1]:     return self.load_wsgiapp()
2013-10-14T02:33:15.360482+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
2013-10-14T02:33:15.360482+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
2013-10-14T02:33:15.360658+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
2013-10-14T02:33:15.360658+00:00 app[web.1]:     return util.import_app(self.app_uri)
2013-10-14T02:33:15.360658+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/util.py", line 354, in import_app
2013-10-14T02:33:15.360658+00:00 app[web.1]: ImportError: No module named xxxxx

2013-10-14T02:33:15.360658+00:00 app[web.1]:     __import__(module)
2013-10-14T02:33:15.360658+00:00 app[web.1]: Traceback (most recent call last):
2013-10-14T02:33:15.360658+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495, in spawn_worker
2013-10-14T02:33:15.360658+00:00 app[web.1]:     worker.init_process()
2013-10-14T02:33:15.360658+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 106, in init_process
2013-10-14T02:33:15.360658+00:00 app[web.1]:     self.wsgi = self.app.wsgi()
2013-10-14T02:33:15.360814+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
2013-10-14T02:33:15.360814+00:00 app[web.1]:     self.callable = self.load()
2013-10-14T02:33:15.360814+00:00 app[web.1]:     return util.import_app(self.app_uri)
2013-10-14T02:33:15.360814+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
2013-10-14T02:33:15.360814+00:00 app[web.1]:     return self.load_wsgiapp()
2013-10-14T02:33:15.360814+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
2013-10-14T02:33:15.360814+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/util.py", line 354, in import_app
2013-10-14T02:33:15.360814+00:00 app[web.1]:     __import__(module)
2013-10-14T02:33:15.360814+00:00 app[web.1]: ImportError: No module named xxxxx

2013-10-14T02:33:15.360814+00:00 app[web.1]: 2013-10-14 02:33:15 [7] [INFO] Worker exiting (pid: 7)
2013-10-14T02:33:15.506842+00:00 app[web.1]: 2013-10-14 02:33:15 [2] [INFO] Shutting down: Master
2013-10-14T02:33:15.506842+00:00 app[web.1]: 2013-10-14 02:33:15 [2] [INFO] Reason: Worker failed to boot.
2013-10-14T02:33:16.807631+00:00 heroku[web.1]: Process exited with status 3
2013-10-14T02:33:16.808898+00:00 heroku[web.1]: State changed from starting to crashed
2013-10-14T02:33:16.810378+00:00 heroku[web.1]: State changed from crashed to starting
2013-10-14T02:33:20.292603+00:00 heroku[web.1]: Starting process with command `gunicorn xxxxx:app`
2013-10-14T02:33:21.415595+00:00 app[web.1]: 2013-10-14 02:33:21 [2] [INFO] Starting gunicorn 18.0
2013-10-14T02:33:21.424049+00:00 app[web.1]: 2013-10-14 02:33:21 [2] [INFO] Listening at: http://0.0.0.0:21832 (2)
2013-10-14T02:33:21.424357+00:00 app[web.1]: 2013-10-14 02:33:21 [2] [INFO] Using worker: sync
2013-10-14T02:33:21.451415+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495, in spawn_worker
2013-10-14T02:33:21.451415+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
2013-10-14T02:33:21.446578+00:00 app[web.1]: 2013-10-14 02:33:21 [7] [INFO] Booting worker with pid: 7
2013-10-14T02:33:21.451415+00:00 app[web.1]: 2013-10-14 02:33:21 [7] [ERROR] Exception in worker process:
2013-10-14T02:33:21.451415+00:00 app[web.1]: Traceback (most recent call last):
2013-10-14T02:33:21.451415+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 106, in init_process
2013-10-14T02:33:21.451415+00:00 app[web.1]:     return self.load_wsgiapp()
2013-10-14T02:33:21.451582+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
2013-10-14T02:33:21.451582+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/util.py", line 354, in import_app
2013-10-14T02:33:21.451415+00:00 app[web.1]:     self.callable = self.load()
2013-10-14T02:33:21.451415+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
2013-10-14T02:33:21.451582+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 495, in spawn_worker
2013-10-14T02:33:21.451582+00:00 app[web.1]:     worker.init_process()
2013-10-14T02:33:21.451582+00:00 app[web.1]:     return util.import_app(self.app_uri)
2013-10-14T02:33:21.451582+00:00 app[web.1]:     __import__(module)
2013-10-14T02:33:21.451415+00:00 app[web.1]:     worker.init_process()
2013-10-14T02:33:21.451765+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 114, in wsgi
2013-10-14T02:33:21.451765+00:00 app[web.1]:     return util.import_app(self.app_uri)
2013-10-14T02:33:21.451765+00:00 app[web.1]: 2013-10-14 02:33:21 [7] [INFO] Work
er exiting (pid: 7)
2013-10-14T02:33:21.451582+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 106, in init_process
2013-10-14T02:33:21.451582+00:00 app[web.1]: Traceback (most recent call last):
2013-10-14T02:33:21.451765+00:00 app[web.1]:     self.callable = self.load()
2013-10-14T02:33:21.451765+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 62, in load
2013-10-14T02:33:21.451765+00:00 app[web.1]:     return self.load_wsgiapp()
2013-10-14T02:33:21.451415+00:00 app[web.1]:     self.wsgi = self.app.wsgi()
2013-10-14T02:33:21.451765+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 49, in load_wsgiapp
2013-10-14T02:33:21.451582+00:00 app[web.1]:     self.wsgi = self.app.wsgi()
2013-10-14T02:33:21.451765+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/util.py", line 354, in import_app
2013-10-14T02:33:21.451765+00:00 app[web.1]:     __import__(module)
2013-10-14T02:33:21.451582+00:00 app[web.1]: ImportError: No module named xxxxx
2013-10-14T02:33:21.451765+00:00 app[web.1]: ImportError: No module named xxxxx

2013-10-14T02:33:21.614863+00:00 app[web.1]: 2013-10-14 02:33:21 [2] [INFO] Shutting down: Master
2013-10-14T02:33:21.614999+00:00 app[web.1]: 2013-10-14 02:33:21 [2] [INFO] Reason: Worker failed to boot.
2013-10-14T02:33:22.891685+00:00 heroku[web.1]: State changed from starting to crashed
2013-10-14T02:33:22.880946+00:00 heroku[web.1]: Process exited with status 3
2013-10-14T02:33:56.309306+00:00 heroku[api]: Scale to web=1 by EMAIL@email.com
2013-10-14T02:34:38.709036+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=xxxxx.herokuapp.com fwd="68.8.230.232" dyno= connect= service= status=503 bytes=
2013-10-14T02:35:26.497306+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=xxxxx.herokuapp.com fwd="68.8.230.232" dyno= connect= service= status=503 bytes=
2013-10-14T02:35:29.339881+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=xxxxx.herokuapp.com fwd="68.8.230.232" dyno= connect= service= status=503 bytes=
2013-10-14T02:34:35.682537+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=xxxxx.herokuapp.com fwd="68.8.230.232" dyno= connect= service= status=503 bytes=
2013-10-14T02:34:36.970547+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=xxxxx.herokuapp.com fwd="68.8.230.232" dyno= connect= service= status=503 bytes=
2013-10-14T02:35:27.860267+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=xxxxx.herokuapp.com fwd="68.8.230.232" dyno= connect= service= status=503 bytes=
2013-10-14T02:36:45.618318+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=xxxxx.herokuapp.com fwd="68.8.230.232" dyno= connect= service= status=503 bytes=
2013-10-14T02:37:13.316055+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=xxxxx.herokuapp.com fwd="68.8.230.232" dyno= connect= service= status=503 bytes=
2013-10-14T02:37:16.020891+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=xxxxx.herokuapp.com fwd="68.8.230.232" dyno= connect= service= status=503 bytes=
2013-10-14T02:37:54.849200+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=xxxxx.herokuapp.com fwd="68.8.230.232" dyno= connect= service= status=503 bytes=
2013-10-14T02:37:14.772425+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=xxxxx.herokuapp.com fwd="68.8.230.232" dyno= connect= service= status=503 bytes=

我不太确定这些都是什么意思。但我猜测这与gunicorn连接有关?有人可以帮忙指出问题并告诉我如何解决吗?

注:没有 ImportError: No module named xxxxx --> xxxxx 是我的应用程序名称,也是我在 procfile 中使用的名称(例如xxxxx:app)。这是否是问题所在?

编辑:这是一个 Flask 应用程序,以下是我目前拥有的结构:

   -static
      --css
      --img
      --js
   -templates
      --index.html
      --layout.html
   -.gitignore
   -Procfile
   -requirements.txt
   -app.py

我的 Procfile:

web: gunicorn app:app

编辑2:这是我的app.py文件

from flask import Flask, render_template

app = Flask(__name__)


@app.route('/')
def main():
    return render_template("index.html")


@app.route('/projects')
def projects():
    ...

...

if __name__ == '__main__':
    app.run()

请添加一个项目目录和文件结构的描述,详细说明app对象在哪里定义。 - Miguel Grinberg
已添加。我正在使用基本的Flask结构。 - user2872988
你的“xxxxx”包在哪里?这就是你告诉gunicorn要导入的内容。 - Miguel Grinberg
哦,我明白了。好的,它会指向app.py文件吗?我刚刚这样做了,但似乎还是不起作用。 - user2872988
我也不认为我理解你的问题... xxxxx 是我的应用程序名称,它没有在任何地方定义。我需要在某个地方定义它吗? - user2872988
1个回答

14

当您配置Procfile时,应写为:

web: gunicorn xxxxx:app

你正在给Heroku下达命令。在转向Heroku之前,您应确保可以在开发机器上运行此命令。

您传递给gunicorn的 "xxxxx" 参数是包或模块,gunicorn将导入该包或模块以获取您的应用程序对象。如果您在app.py中创建应用程序,则应为"app"(不应包括.py扩展名)。您遇到的错误是因为gunicorn发出了一个 import xxxxxx 命令,显然失败了,因为您没有定义具有该名称的包或模块。

在冒号的另一侧,您必须放置您的应用程序名称。例如,如果您在app.py文件中有以下代码:

app = Flask(__name__)

那么你的应用程序名称是 app。为了进行此设置,Procfile 应该如下所示:

web: gunicorn app:app

然后,gunicorn将发出import app命令(左边的app),并在其中查找名为app的符号(右边的app)。

希望这可以帮到您。


1
嗨,那正是我所拥有的(已更新第一篇帖子),但似乎它并没有起作用... - user2872988
请添加一个更新的错误日志,其中包含更改为“app: app”后出现的错误。 - Miguel Grinberg
1
有时候,如果import app语句在if __name__ == "__main__"块内部,当导入模块时它可能永远不会被执行。为了解决这个问题,import app语句应该移动到if块之外,这样gunicorn就可以找到app对象。 - coolharsh55

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