简体   繁体   中英

Deploying Flask app to Heroku - “Connection in Use”

I'm deploying a simple Flask app to Heroku - (first time using Flask and Heroku both). When I try to deploy, I get an "Application Error" and the page tells me to try again in a few minutes. The logs state - the "connection [is] in use", retries a few times and then the worker exits (I can post the logs if that is helpful).

My demo.py file:

import flask, flask.views
import os
import urllib2
from bs4 import BeautifulSoup

opener = urllib2.build_opener()

app = flask.Flask(__name__)

app.secret_key = "bacon"

class View(flask.views.MethodView):
    def get(self):
        return flask.render_template('index.html')


def post(self):
    url = (flask.request.form['url'])
    ourUrl = opener.open(url).read()
    soup = BeautifulSoup(ourUrl)
    title = soup.title.text
    recipe = soup.find("div", {"id": "recipe"}).getText()
    flask.flash(title)
    flask.flash(recipe)
    return self.get()


app.add_url_rule('/', view_func=View.as_view('main'), methods=['GET', 'POST'])

app.debug = True
app.run()

My procfile is: web: gunicorn demo:app

If I change the procfile to web: python demo.py, I am able to run the app locally using Foreman but still cannot deploy to Heroku.

Any help is very much appreciated. This is my first time doing this!!

Thank you.

I figured it out. Need to add the following before app.run()

if __name__ == "__main__":

Now it runs fine on Heroku.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM