【问题标题】:Python Flask Web API [Heroku]: It runs locally but shows Application Error when deployedPython Flask Web API [Heroku]:它在本地运行,但在部署时显示应用程序错误
【发布时间】:2017-09-21 10:57:52
【问题描述】:

我在 StackOverflow 上进行了搜索,寻找可以帮助我使 Web API 正常工作的答案,但不知何故,我仍然没有我需要的答案。我已经遵循了所有必要的步骤,直到我可以将项目部署到 heroku(通过 Github),并且它没有显示构建错误。 Procfile 已经包含在内,我的 requirements.txt 也是如此。请看下面:

项目目录:

app-directory/
  |-main-api.py
  |-Procfile
  |-requirements.txt
  |-..other files..

Procfile(更新):

web: gunicorn --bind 127.0.0.1:5000 main-api:app

requirements.txt:

  • 烧瓶
  • PyMongo
  • 独角兽

在 main-api.py...

@app.route('/', methods=['GET'])
def get_index():
    customers = mongo.db.customers

    output = []

    for c in customers.find():
        output.append({'_id': c['_id'], 'first_name': c['first_name'],
                   'last_name': c['last_name'],
                   'date_of_birth': c['date_of_birth'],
                   'is_online': c['is_online']})

    return jsonify({'results': output})

最后...在 Heroku 中运行 Web 项目:

Heroku Web - Python-Flask-Error

任何有类似经验或对此问题有正确解决方案的人,如果你能分享它,不胜感激。感谢百万人!

附:抱歉,我忘了添加 Heroku 的日志:

2017-04-24T18:22:17.118866+00:00 heroku[web.1]: Starting process with command `gunicorn --bind 127.0.0.1:5000 main-api:app`
2017-04-24T18:22:20.224956+00:00 app[web.1]: [2017-04-24 18:22:20 +0000] [4] [INFO] Starting gunicorn 19.7.1
2017-04-24T18:22:20.225914+00:00 app[web.1]: [2017-04-24 18:22:20 +0000] [4] [INFO] Listening at: http://127.0.0.1:5000 (4)
2017-04-24T18:22:20.226111+00:00 app[web.1]: [2017-04-24 18:22:20 +0000] [4] [INFO] Using worker: sync
2017-04-24T18:22:20.230648+00:00 app[web.1]: [2017-04-24 18:22:20 +0000] [9] [INFO] Booting worker with pid: 9
2017-04-24T18:22:20.332390+00:00 app[web.1]: [2017-04-24 18:22:20 +0000] [10] [INFO] Booting worker with pid: 10
2017-04-24T18:23:17.510473+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2017-04-24T18:23:17.510556+00:00 heroku[web.1]: Stopping process with SIGKILL
2017-04-24T18:23:17.686086+00:00 heroku[web.1]: State changed from starting to crashed
2017-04-24T18:23:17.667150+00:00 heroku[web.1]: Process exited with status 137
2017-04-24T18:23:19.078229+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=cx-machinelearning.herokuapp.com request_id=1ce56539-862b-4355-ba36-a6453a646890 fwd="101.127.102.75" dyno= connect= service= status=503 bytes= protocol=https
2017-04-24T18:23:19.971719+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=cx-machinelearning.herokuapp.com request_id=55d8cad7-3967-466b-b78c-d9e377be8cbc fwd="101.127.102.75" dyno= connect= service= status=503 bytes= protocol=https

我已检查以确保已在 main_api.py 的 ma​​in 部分中设置了 debug=True。

【问题讨论】:

  • 您是否尝试检查日志?如果您使用 heroku cli,您可以使用 heroku logs 检查日志,如果您喜欢 UI,则可以直接从 heroku 应用仪表板查看日志
  • 我已经从 Heroku 的日志中添加了日志段。

标签: python api heroku web flask


【解决方案1】:

看起来 Heroku 负载均衡器无法访问您的应用程序,因为它仅在 localhost(IP 地址127.0.0.1)上侦听。此外,Heroku 动态分配端口号并将当前值分配给PORT 环境变量。

请尝试以下 Procfile:

web: gunicorn --bind 0.0.0.0:$PORT main-api:app

【讨论】:

  • 感谢@adam-brytek 的帮助!它就像一个魅力。将此设置为答案。我希望有人能从这个问题中受益。
  • 不客气@ShermanChen,但看起来你最后没有勾选答案标记:)
  • 是的,完成了。抱歉,这是我第一次主动参与 stackoverflow。希望在其他领域提供答案方面做出自己的贡献。
  • 我被同样的错误困扰了一周,浪费了数十个小时在线寻找答案! heroku官方文档没有帮助,非常感谢!
  • 我也用过这个,但仍然遇到问题?还有其他选择吗?
【解决方案2】:

对我来说,问题在于我在 Procfile 中写道:

web: gunicorn appname:app

其中 appname 是我在 Heroku 网站中使用的应用程序名称,而不是:

web: gunicorn MainScriptName:app

这意味着获取 yourscriptname.py 并在没有扩展名的情况下复制它,所以你会得到yourscriptname:app

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-30
    • 2020-06-06
    • 2020-08-01
    • 2019-09-17
    • 2019-05-24
    • 1970-01-01
    • 2022-07-15
    • 1970-01-01
    相关资源
    最近更新 更多