问题描述
我正在尝试在heroku上部署Flask应用程序.我已经到了构建和部署应用程序的地步,但是当我尝试转到URL时,该应用程序因以下错误而超时.
I'm trying to deploy a flask app on heroku. I've gotten to the point where the app builds and deploys, but when I try to go to the URL, the app times out with the following error.
Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
我认为问题出在我的procfile上.它只有一行.
I think the problem is with my procfile. It has one line.
web: python add_entry3.py
其他人的procfile看起来像这样:
Other people have procfiles that look like this:
web: gunicorn app:app
这只是一个玩具应用程序,我不在乎性能,因此我认为我不需要在网络服务器上使用gunicorn.我应该在应用程序的文件名(add_entry3.py)之后加上冒号和命令吗?
This is just a toy app and I don't care about performance so I don't think I need to use gunicorn for the web server. Should I be putting a colon and command after my app's file name (add_entry3.py)?
推荐答案
您的flask应用很可能在Heroku期望的端口和接口上没有应答.默认情况下,Flask仅 侦听127.0.0.1,我认为在端口5000上.Heroku向您的应用程序传递了一个 PORT
环境变量,您需要告诉Flask侦听在所有界面上.
Most likely your flask app isn't answering on the port and interface the Heroku expects. By default, Flask only listens on 127.0.0.1, and I think on port 5000. Heroku passes your app a PORT
environment variable and you'd need to tell Flask to listen on all interfaces.
但是,除了性能以外,还有其他一些原因需要避免Flask的默认调试服务器用于生产代码.有内存泄漏,有安全隐患,实际上……只是不这样做.将gunicorn添加到您的requirements.txt文件中,然后使用它.
But there are reasons other than performance you want to avoid Flask's default debug server for production code. It's got memory leaks, there are security implications, and really ... just don't do it. Add gunicorn to your requirements.txt and use that.
但是如果您必须使用Flask测试/调试服务器,请将您的 app.run()
调用更改为以下内容:
But if you must use the Flask test/debug server, change your app.run()
call to something like this:
app.run(host='0.0.0.0', port=int(os.environ.get("PORT", 5000)))
这篇关于适用于Heroku上Flask App的Procfile的正确格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!