OSError: [Errno 98] Address already in use - Heroku

1.9k Views Asked by At

enter image description here

I'm trying to deploy a project https://github.com/kc1/flask-huey-example to heroku to allow usage of a task queue and flask. The project layout is as above. You can also take a look at the github repo which I have updated. The project is running without issue locally on windows (gunicorn is not used locally) .

My Procfile:

web: gunicorn run_app:app
worker: python /app/.heroku/python/bin/huey_consumer run_huey.huey

I had previously gotten an error Failed to find application object 'app' in 'run_app' . I tried to fix that by changing run_app.py to:

from app import create_app, config

if __name__ == '__main__':

    import logging
    logging.basicConfig()
    logging.getLogger().setLevel(logging.DEBUG)

    app = create_app()
    app.run(host='localhost', port=6060, debug=True)


else:


    app = create_app()
    app.run(debug=False)

Unfortunately , now when I push to heroku, I see:

2018-08-16T16:37:59.253114+00:00 app[web.1]: [2018-08-16 16:37:59 +0000] [8] [ERROR] Exception in worker process
2018-08-16T16:37:59.253118+00:00 app[web.1]: Traceback (most recent call last):
2018-08-16T16:37:59.253121+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2018-08-16T16:37:59.253123+00:00 app[web.1]:     worker.init_process()
2018-08-16T16:37:59.253124+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process
2018-08-16T16:37:59.253126+00:00 app[web.1]:     self.load_wsgi()
2018-08-16T16:37:59.253128+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
2018-08-16T16:37:59.253130+00:00 app[web.1]:     self.wsgi = self.app.wsgi()
2018-08-16T16:37:59.253132+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2018-08-16T16:37:59.253133+00:00 app[web.1]:     self.callable = self.load()
2018-08-16T16:37:59.253135+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
2018-08-16T16:37:59.253137+00:00 app[web.1]:     return self.load_wsgiapp()
2018-08-16T16:37:59.253139+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
2018-08-16T16:37:59.253140+00:00 app[web.1]:     return util.import_app(self.app_uri)
2018-08-16T16:37:59.253142+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app
2018-08-16T16:37:59.253144+00:00 app[web.1]:     __import__(module)
2018-08-16T16:37:59.253146+00:00 app[web.1]:   File "/app/run_app.py", line 21, in <module>
2018-08-16T16:37:59.253148+00:00 app[web.1]:     app.run(debug=False)
2018-08-16T16:37:59.253149+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 841, in run
2018-08-16T16:37:59.253151+00:00 app[web.1]:     run_simple(host, port, self, **options)
2018-08-16T16:37:59.253153+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/serving.py", line 814, in run_simple
2018-08-16T16:37:59.253155+00:00 app[web.1]:     inner()
2018-08-16T16:37:59.253157+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/serving.py", line 774, in inner
2018-08-16T16:37:59.253159+00:00 app[web.1]:     fd=fd)
2018-08-16T16:37:59.253161+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/serving.py", line 666, in make_server
2018-08-16T16:37:59.253162+00:00 app[web.1]:     passthrough_errors, ssl_context, fd=fd)
2018-08-16T16:37:59.253164+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/serving.py", line 577, in __init__
2018-08-16T16:37:59.253165+00:00 app[web.1]:     self.address_family), handler)
2018-08-16T16:37:59.253167+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/socketserver.py", line 453, in __init__
2018-08-16T16:37:59.253169+00:00 app[web.1]:     self.server_bind()
2018-08-16T16:37:59.253171+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/http/server.py", line 136, in server_bind
2018-08-16T16:37:59.253173+00:00 app[web.1]:     socketserver.TCPServer.server_bind(self)
2018-08-16T16:37:59.253181+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/socketserver.py", line 467, in server_bind
2018-08-16T16:37:59.253183+00:00 app[web.1]:     self.socket.bind(self.server_address)

How can I fix this?

2

There are 2 best solutions below

0
On

The port you are trying to bind you application to is already being used by another app. Close the other app and re-deploy

0
On

I got the same error when trying to run a Flask application on Heroku and solved it by removing the

app.run

statement entirely from my app file (mine is named app.py), for example your run_app.py.

My Procfile:

web: gunicorn -b 0.0.0.0:$PORT app:app

When I want to run the app locally, I run in terminal (MacOS zsh)

FLASK_ENV=development flask run