Likai Ren
Likai Ren

Reputation: 125

gunicorn cannot start flask

I cannot start flask by gunicorn. When I input "gunicorn run:app", the server gives me errors. But I check that there is no service using 127.0.0.1:8000. And I can use "python run.py" to run this application.

root@ip-172-31-18-230:/var/www/vamk.help/vamk# gunicorn run:app
[2016-05-11 02:13:09 +0000] [20288] [INFO] Starting gunicorn 19.4.5
[2016-05-11 02:13:09 +0000] [20288] [INFO] Listening at: http://127.0.0.1:8000 (                                                                                                 20288)
[2016-05-11 02:13:09 +0000] [20288] [INFO] Using worker: sync
[2016-05-11 02:13:09 +0000] [20293] [INFO] Booting worker with pid: 20293
[2016-05-11 02:13:10 +0000] [20293] [ERROR] Exception in worker process:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 515, i                                                                                                 n spawn_worker
    worker.init_process()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 1                                                                                                 22, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 1                                                                                                 30, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/base.py", line 67, i                                                                                                 n wsgi
    self.callable = self.load()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 65                                                                                                 , in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 52                                                                                                 , in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/util.py", line 357, in i                                                                                                 mport_app
    __import__(module)
  File "/var/www/vamk.help/vamk/run.py", line 3, in <module>
    app.run(threaded=True, debug=app.config['DEBUG'], port=app.config['PORT'])
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 772, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 677, i                                                                                                 n run_simple
    s.bind((hostname, port))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 98] Address already in use
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 515, i                                                                                                 n spawn_worker
    worker.init_process()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 1                                                                                                 22, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 1                                                                                                 30, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/base.py", line 67, i                                                                                                 n wsgi
    self.callable = self.load()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 65                                                                                                 , in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 52                                                                                                 , in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python2.7/dist-packages/gunicorn/util.py", line 357, in i                                                                                                 mport_app
    __import__(module)
  File "/var/www/vamk.help/vamk/run.py", line 3, in <module>
    app.run(threaded=True, debug=app.config['DEBUG'], port=app.config['PORT'])
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 772, in run
    run_simple(host, port, self, **options)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 677, i                                                                                                 n run_simple
    s.bind((hostname, port))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 98] Address already in use

run.py

from app import app

app.run(debug=app.config['DEBUG'], port=app.config['PORT'])

Upvotes: 3

Views: 4725

Answers (2)

Ninad Kulkarni
Ninad Kulkarni

Reputation: 476

This is what worked for me. Change your run.py file as follows

from app import app

if __name__ == "__main__":
    app.run(debug=app.config['DEBUG'], port=app.config['PORT'])

Upvotes: 0

user6025378
user6025378

Reputation:

To use flask with gunicorn you dont have to run the app (app.run()) since it is a single blocking process which is used only for testing.

Instead pass the flask app module that initiates the variable app (if it is a single file, pass the filename) as argument to gunicorn instead of the external run.py.

This is the right way to use gunicorn with flask.

gunicorn  -b HOST:PORT <MODULE_NAME>:app

Replace MODULE_NAME with the correct module or file name. Use -b flag to set the IP address and port number.

Upvotes: 6

Related Questions