首页 > 解决方案 > Python Heroku 显示 R10(启动超时)错误

问题描述

我正在尝试在 Heroku 中托管 Flask 应用程序,但出现启动超时错误 (R10)。
其他答案告诉我在运行中使用 host="0.0.0.0" ,但它仍然无法正常工作。
我正在使用 Heroku CLI、Windows (CMD)、Python 3.7.7。

错误

2021-10-10T18:05:14.522910+00:00 heroku[web.1]: Starting process with command `waitress-serve app:app`
2021-10-10T18:05:15.701238+00:00 app[web.1]: INFO:waitress:Serving on http://0.0.0.0:8080
2021-10-10T18:06:14.938492+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2021-10-10T18:06:15.273888+00:00 heroku[web.1]: Stopping process with SIGKILL
2021-10-10T18:06:15.448673+00:00 heroku[web.1]: Process exited with status 137
2021-10-10T18:06:15.535916+00:00 heroku[web.1]: State changed from starting to crashed
2021-10-10T18:06:15.574210+00:00 heroku[web.1]: State changed from crashed to starting
2021-10-10T18:06:18.322384+00:00 heroku[web.1]: Starting process with command `waitress-serve app:app`
2021-10-10T18:06:19.508990+00:00 app[web.1]: INFO:waitress:Serving on http://0.0.0.0:8080
2021-10-10T18:06:35.678582+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/favicon.ico" host=mhd-project.herokuapp.com request_id=5e553adf-0b86-4663-896f-cc948cac3e62 fwd="79.252.124.156" dyno= connect= service= status=503 bytes= protocol=https
2021-10-10T18:07:03.746794+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=mhd-project.herokuapp.com request_id=67e98e81-aedf-484c-af6f-61500d797daa fwd="79.252.124.156" dyno= connect= service= status=503 bytes= protocol=https
2021-10-10T18:07:06.871218+00:00 heroku[router]: at=error code=H20 desc="App boot timeout" method=GET path="/" host=mhd-project.herokuapp.com request_id=5c7c269b-1261-4c99-b952-f559314f2460 fwd="79.252.124.156" dyno= connect= service= status=503 bytes= protocol=https
2021-10-10T18:07:19.041046+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2021-10-10T18:07:19.174484+00:00 heroku[web.1]: Stopping process with SIGKILL
2021-10-10T18:07:19.346300+00:00 heroku[web.1]: Process exited with status 137
2021-10-10T18:07:19.393363+00:00 heroku[web.1]: State changed from starting to crashed
2021-10-10T18:07:49.835669+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=mhd-project.herokuapp.com request_id=a05bccea-a6e9-4f85-b9d6-0c9667ca2e50 fwd="79.252.124.156" dyno= connect= service= status=503 bytes= protocol=https

应用程序.py

from flask import Flask, render_template, request, g, flash, redirect, jsonify
import sqlite3
from flask_login import LoginManager, login_user, UserMixin, current_user, logout_user
from datetime import datetime,timedelta, date

...

if __name__ == "__main__":
      app.run(host='0.0.0.0')

要求.txt

click==7.1.2
Jinja2==2.11.2
itsdangerous==1.1.0
Werkzeug==1.0.1
Flask==1.1.2
SQLAlchemy==1.3.22
Flask-SQLAlchemy==2.4.4
waitress==2.0.0
Flask-Login==0.5.0

档案

web: waitress-serve app:app

标签: pythonpython-3.xheroku

解决方案


您需要从 Heroku 获取端口配置,并在您的应用程序上使用它:

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.run(host='0.0.0.0', port=port)

参考:https ://blog.heroku.com/python_and_django


推荐阅读