metersk
metersk

Reputation: 12539

Flask app won't run on Heroku

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.

Upvotes: 0

Views: 1466

Answers (1)

Sean Vieira
Sean Vieira

Reputation: 160073

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.

Upvotes: 3

Related Questions