Flask应用程序不能在Heroku上运行

时间:2014-01-12 19:50:14

标签: python heroku flask

当我从Heroku本地运行它时运行正常。在日志中,我得到的错误是超时错误。

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)

我的procfile说:

web: python routes.py

以下是一些新的错误日志:

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

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

1 个答案:

答案 0 :(得分:3)

正如日志所述:

  

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

您没有绑定到提供的PORT变量,因此Heroku会终止您的程序。如果您只想玩Heroku + Flask,只需将__main__行更改为:

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

如果这需要处理两个以上的并发连接,您可能希望在deploying into standard WSGI containers上看到Flask的文档部分。