You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Heroku+Python+Gunicorn+Flask部署Web应用遇启动错误求助

部署Flask+Gunicorn到Heroku时的H10崩溃问题排查

错误日志

2022-12-07T00:43:06.517424+00:00 app[web.1]: raise HaltServer(reason, self.WORKER_BOOT_ERROR)
2022-12-07T00:43:06.517467+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
2022-12-07T00:43:06.672564+00:00 heroku[web.1]: Process exited with status 1
2022-12-07T00:43:06.728584+00:00 heroku[web.1]: State changed from up to crashed
2022-12-07T00:43:19.882600+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=spo-bet-an.herokuapp.com request_id=b11bfee6-9bdd-4a2d-a446-92a927818e54 fwd="65.189.83.94" dyno= connect= service= status=503 bytes= protocol=https
2022-12-07T00:43:20.529051+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=spo-bet-an.herokuapp.com request_id=d1e24818-b06e-4cec-b254-11812b71dd9c fwd="65.189.83.94" dyno= connect= service= status=503 bytes= protocol=https

相关文件内容

初始home.py

import os
from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello World'

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

初始Procfile

web: gunicorn --bind 0.0.0.0:${PORT} home:app

尝试过的Procfile与对应home.py

Procfile:

web: gunicorn home:app

对应home.py:

import os
from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello World'

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

requirements.txt内容

async-generator==1.10
attrs==22.1.0
certifi==2022.9.24
cffi==1.15.1
charset-normalizer==2.1.1
click==8.1.3
colorama==0.4.6
dnspython==2.2.1
Flask==2.2.2
gunicorn==20.1.0
h11==0.14.0
heroku==0.1.4
idna==3.4
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.1
outcome==1.2.0
packaging==21.3
pycparser==2.21
pymongo==4.3.3
pyparsing==3.0.9
PySocks==1.7.1
python-dateutil==1.5
python-dotenv==0.21.0
requests==2.28.1
selenium==4.6.1
sniffio==1.3.0
sortedcontainers==2.4.0
tqdm==4.64.1
trio==0.22.0
trio-websocket==0.9.2
urllib3==1.26.13
webdriver-manager==3.8.5
Werkzeug==2.2.2
wsproto==1.2.0
uwsgi
gunicorn

已尝试操作

  • 调整Procfile的端口绑定参数
  • 在requirements.txt中增删gunicorn、uwsgi条目
  • 尝试用uwsgi替代gunicorn启动应用

补充环境信息

  • 开发环境为Windows系统
  • 未使用虚拟环境
  • 所有项目文件在同一目录
  • 未修改系统环境变量

排查思路与解决方案

1. 简化依赖列表,排除冲突

当前requirements.txt包含大量非核心依赖(如selenium、pymongo、trio等),这些依赖可能在Heroku的Linux环境中安装失败或与gunicorn/Flask冲突:

  • 先创建极简版requirements.txt,仅保留核心依赖:
Flask==2.2.2
gunicorn==20.1.0
  • 部署测试,若成功启动,再逐步添加必要依赖,每次添加后部署验证,定位冲突包。

2. 修正Procfile写法

Heroku会自动注入PORT环境变量,推荐使用最简启动命令:

web: gunicorn home:app

无需手动指定0.0.0.0:${PORT},gunicorn会自动读取PORT变量完成端口绑定。

3. 优化home.py的启动逻辑

确保app对象在模块级别可被gunicorn正确导入,同时避免生产环境开启debug模式:

import os
from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello World'

if __name__ == '__main__':
    # 本地启动时默认端口5000,兼容Heroku的PORT变量
    port = int(os.environ.get('PORT', 5000))
    # 仅本地开发时开启debug
    debug_mode = os.environ.get('DEBUG', 'False').lower() == 'true'
    app.run(host='0.0.0.0', port=port, debug=debug_mode)

注:debug模式在生产环境存在安全风险,且Heroku的dyno重启机制与debug自动重载不兼容。

4. 查看完整启动日志

H10错误仅表明应用崩溃,需查看更详细的启动异常:

heroku logs --tail --app spo-bet-an

重点关注gunicorn worker启动时的具体报错(如依赖导入失败、版本不兼容等)。

5. 消除Windows与Linux的环境差异

Windows开发环境下未使用虚拟环境,可能导致requirements.txt包含Windows特有的依赖(如colorama),在Heroku的Linux环境中可能引发问题:

  • 本地使用虚拟环境,仅安装项目必需依赖,执行pip freeze > requirements.txt生成干净的依赖列表。

6. 移除uwsgi(除非明确需要)

无需同时安装gunicorn和uwsgi,Heroku上gunicorn已足够稳定,直接从requirements.txt中删除uwsgi相关条目。


内容的提问来源于stack exchange,提问作者jaysonhayes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 03:46:12