Python Flask Web API [Heroku]:它在本地运行但在部署时显示应用程序错误

时间:2017-04-24 16:39:19

标签: python api heroku web flask

我已经在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
  • Gunicorn

在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

任何具有相似经验或有正确解决此问题的人,如果您能分享,请表示赞赏。感谢一百万人!

P.S。抱歉,我忘了从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的部分设置了debug = True。

1 个答案:

答案 0 :(得分:2)

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

请尝试使用以下Procfile:

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