Flask应用程序不会在Heroku上运行 [英] Flask app won't run on Heroku

查看:234
本文介绍了Flask应用程序不会在Heroku上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从Heroku本地运行它时,它运行的很好。在日志中,我得到的错误是一个超时错误。

  from flask import Flask,render_template,request 
import import
import json
$ b $ app = Flask(__ name__)

def decrementList(words):
for w in [words] + [words [:-x] for x in range(1,len(words))]:
url ='http://ws.spotify.com/search/1/track.json?q='
request = requests.get(url +%20.join(w))

json_dict = json.loads(request.content)
track_title =''.join(w)

for json_dict [tracks]:
如果track [name]。 $ b returnhttp://open.spotify.com/track/+ track [href] [14:],单词[len(w):],track [href] [14:]

返回抱歉,找不到(更多)曲目匹配结果!,None,

@ app.route('/')
def home()
message = request.args.get('q','').split()
first_a rg =''.join(message)
playlist = []
results = []
while message:
href,new_list,for_playlist = decrementList(message)
message = new_list
results.append(href)

playlist.append(for_playlist)
$ b playlist_link =','。join(playlist)


返回render_template('home.html',first_arg = first_arg,results = results,playlist_link = playlist_link)

if __name__ =='__main__':
app。运行(debug = False)

我的procfile说:

  web:python routes.py 

是一些新的错误日志:

  2014-01-14T02:47:38.042655 + 00:00 heroku [web.1]:进程退出状态137 
2014-01-14T02:47:41.346999 + 00:00 heroku [web.1]:用命令`python routes.py`启动进程
2014-01-14T02:47 :42.443673 + 00:00 app [web.1]:回溯(最近一次通话最后):
2014-01-14T02:47:42.443673 + 00:00 app [web.1]:文件/app/.heroku/python/lib/python2.7/logging/__init__.py,行851,放出
2014-01-14T02:47:42.443673 + 00:00 app [web.1]:msg = self.format(record)
2014-01-14T02:47:42.443673 + 00:00 app [web .1]:文件/app/.heroku/python/lib/python2.7/logging/__init__.py,第724行,格式为
2014-01-14T02:47:42.443673 + 00:00 [web.1]:return fmt.format(record)
2014-01-14T02:47:42.443673 + 00:00 app [web.1]:文件/app/.heroku/python/lib/python2 .7 / logging / __init__.py,第464行,格式为
2014-01-14T02:47:42.443673 + 00:00 app [web.1]:record.message = record.getMessage()
2014-01-14T02:47:42.443673 + 00:00 app [web.1]:文件/app/.heroku/python/lib/python2.7/logging/__init__.py,第328行,在getMessage
2014-01-14T02:47:42.443673 + 00:00 app [web.1]:msg = msg%self.args
2014-01-14T02:47:42.443673 + 00:00 app [ web.1]:TypeError:%d格式:需要一个数字,不是str
2014-01-14T02:47:42.444029 + 00:00应用[web.1]:从文件_internal.py,第87行记录

看起来像这些新的日志指向与heroku有关的东西。显然,我不确定。

解决方案

作为日志状态:


错误R10(引导超时) - > Web进程无法在启动后的60秒内绑定到$ PORT

你没有绑定到提供的 PORT 变量,所以Heroku终止你的程序。如果你只是想和Heroku + Flask一起玩,那么只需把你的 __ main __ 一行改为:

 如果__name__ =='__main__':
从os导入environ
app.run(debug = False,port = environ.get(PORT,5000),processes = 2 )

如果需要处理两个以上的并发连接,可能需要查看Flask有关部署到标准WSGI容器的文档。


It runs fine when I run it locally from Heroku. In the logs, the error I'm getting is a timeout error.

from flask import Flask, render_template, request
import requests
import json

app = Flask(__name__)

def decrementList(words):
    for w in [words] + [words[:-x] for x in range(1,len(words))]:
        url = 'http://ws.spotify.com/search/1/track.json?q='
        request = requests.get(url + "%20".join(w))

        json_dict = json.loads(request.content)
        track_title = ' '.join(w)

        for track in json_dict["tracks"]:
            if track["name"].lower() == track_title.lower() and track['href']:
                return "http://open.spotify.com/track/" + track["href"][14:], words[len(w):], track["href"][14:]

    return "Sorry, no (more) track matches found!", None, ""  

@app.route('/')
def home():
    message = request.args.get('q', '').split()
    first_arg = ' '.join(message)
    playlist = []
    results = []
    while message:
        href, new_list, for_playlist = decrementList(message)
        message = new_list
        results.append(href)

        playlist.append(for_playlist)

    playlist_link = ','.join(playlist)


    return render_template('home.html', first_arg=first_arg, results=results, playlist_link=playlist_link)

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

My procfile says this:

web: python routes.py

Here are some new Error Logs:

2014-01-14T02:47:38.042655+00:00 heroku[web.1]: Process exited with status 137
2014-01-14T02:47:41.346999+00:00 heroku[web.1]: Starting process with command `python routes.py`
2014-01-14T02:47:42.443673+00:00 app[web.1]: Traceback (most recent call last):
2014-01-14T02:47:42.443673+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/logging/__init__.py", line 851, in emit
2014-01-14T02:47:42.443673+00:00 app[web.1]:     msg = self.format(record)
2014-01-14T02:47:42.443673+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/logging/__init__.py", line 724, in format
2014-01-14T02:47:42.443673+00:00 app[web.1]:     return fmt.format(record)
2014-01-14T02:47:42.443673+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/logging/__init__.py", line 464, in format
2014-01-14T02:47:42.443673+00:00 app[web.1]:     record.message = record.getMessage()
2014-01-14T02:47:42.443673+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/logging/__init__.py", line 328, in getMessage
2014-01-14T02:47:42.443673+00:00 app[web.1]:     msg = msg % self.args
2014-01-14T02:47:42.443673+00:00 app[web.1]: TypeError: %d format: a number is required, not str
2014-01-14T02:47:42.444029+00:00 app[web.1]: Logged from file _internal.py, line 87

It looks like these new logs are pointing to something heroku related. Obviously, I am not sure though.

解决方案

As the logs state:

Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

You are not binding to the provided PORT variable and so Heroku terminates your program. If you just want to play around with Heroku + Flask then simply change your __main__ line to:

if __name__ == '__main__':
    from os import environ
    app.run(debug=False, port=environ.get("PORT", 5000), processes=2)

If this needs to handle more than two concurrent connections you will probably want to see the section of Flask's docs on deploying into standard WSGI containers.

这篇关于Flask应用程序不会在Heroku上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆