将Python Flask应用部署到Heroku时遇到R10错误

3
我希望使用免费的dyno将一个Flask应用部署到Heroku,目前看来构建成功了:
remote: Git submodules detected, installing:
remote: 
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Python app detected
remote: -----> Installing requirements with pip
remote: 
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing...
remote:        Done: 83.2M
remote: -----> Launching...
remote:        Released v94
remote:        https://MYAPP.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.

访问URL后,检查Heroku日志时,抛出以下错误:

2017-10-25T22:33:09.264449+00:00 heroku[web.1]: Starting process with command `python run.py runserver`
2017-10-25T22:33:15.514250+00:00 app[web.1]:  * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
2017-10-25T22:33:15.519730+00:00 app[web.1]:  * Restarting with stat
2017-10-25T22:33:17.300082+00:00 app[web.1]:  * Debugger is active!
2017-10-25T22:33:17.305442+00:00 app[web.1]:  * Debugger pin code: 146-142-273
2017-10-25T22:34:09.286891+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2017-10-25T22:34:09.286934+00:00 heroku[web.1]: Stopping process with SIGKILL
2017-10-25T22:34:09.469418+00:00 heroku[web.1]: Process exited with status 137
2017-10-25T22:34:09.484569+00:00 heroku[web.1]: State changed from starting to crashed

其他在各种帮助网站上遇到这个问题的人都是通过绑定到Heroku $PORT来解决他们的问题,我也是这样做的:

#!venv/bin/python
import os
from app import app
port = int(os.environ.get('PORT', 33507))
app.run(host='0.0.0.0', port=port, debug=False)

我的Procfile文件:

web: python run.py runserver

可能我正在运行虚拟环境吗?也许我的应用程序大小为83.2M太大了?我有点困惑。提前感谢您的帮助!


我确实尝试过检查,但这不是问题的一部分吗?我启动了动力,等待了一会儿,然后在日志中读取到“错误R10(引导超时)-> Web进程在启动后60秒内未能绑定到$PORT”。 - esreli
尝试使用 port = int(os.environ.get('PORT', 5000))。请参考 https://dev59.com/E2Yr5IYBdhLWcg3wYpUg#13714363。 - yash
尝试过但没有成功的内容 - esreli
这两种技术都不起作用。可能是其他原因吗?为什么控制台会记录:2017-10-25T23:20:57.992995+00:00 app[web.1]: * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit),而我已经指定了另外的端口号?(不是5000) - esreli
我不相信你可以静态地为你的Heroku进程分配端口,这是由Heroku动态分配的,合同要求您绑定到提供的$PORT变量。R10错误的常见原因(根据此处:https://devcenter.heroku.com/articles/error-codes#r10-boot-timeout)是启动时间过长。应用程序在本地启动需要多长时间?尝试使用提供的Procfile在本地启动应用程序,因为Heroku使用它并尝试访问它。您可以尝试使用foreman(https://github.com/ddollar/foreman)或honcho(https://github.com/nickstenning/honcho)。 - oreoluwa
显示剩余3条评论
2个回答

3
尝试将您的Procfile内容更改为以下内容: web:gunicorn run:app -b“0.0.0.0:$ PORT” -w 3 其中run是表示run.py的主应用程序文件的名称。

“-w 3” 对我有用。你能解释一下 “-w 3” 是什么意思吗? - FBI Surveillance Van
刚刚意识到 -w 3 意味着有三个工作进程。不知道为什么这样就解决了问题。 - FBI Surveillance Van

0

尝试更改:

  • Procfile的内容应为:

    web: python run.py
    
  • 端口号从33507更改为8080

  • debug = True。

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