Eli
Eli

Reputation: 38899

Gunicorn Keeps Restarting/Breaking on Flask App

I have a Flask App I'm trying to transition to running through gunicorn. I'm experiencing a lot of problems with this. Here's the run code for my app:

app.run(host=HOST, port=PORT, debug=DEBUG_FLAG)

First, if DEBUG_FLAG == true, the app will never actually start up, but will just keep restarting, and hitting it locally won't work. It just does this over and over:

gunicorn analytics_service:app                                                                                                                         
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader
 * Running on http://127.0.0.1:5000/
 * Restarting with reloader

If I start it with DEBUG_FLAG==False, it will actually start up and serve some requests, but will still break and restart frequently for unknown reasons:

gunicorn analytics_service:app                                                                                                                         (env: BigQueryTest)
 * Running on http://127.0.0.1:5000/
127.0.0.1 - - [28/Aug/2014 08:59:05] "GET /metrics/ctr?location=blah&start_date=2014-05-21&end_date=2014-06-01 HTTP/1.1" 200 -
127.0.0.1 - - [28/Aug/2014 08:59:15] "GET /metrics/ctr?location=blah&start_date=2014-05-21&end_date=2014-06-05 HTTP/1.1" 200 -
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 64693)
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 649, in __init__
    self.handle()
  File "/Users/Eli/.virtualenvs/BigQueryTest/lib/python2.7/site-packages/werkzeug/serving.py", line 200, in handle
    rv = BaseHTTPRequestHandler.handle(self)
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 340, in handle
    self.handle_one_request()
  File "/Users/Eli/.virtualenvs/BigQueryTest/lib/python2.7/site-packages/werkzeug/serving.py", line 231, in handle_one_request
    self.raw_requestline = self.rfile.readline()
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 447, in readline
    data = self._sock.recv(self._rbufsize)
  File "/Users/Eli/.virtualenvs/BigQueryTest/lib/python2.7/site-packages/gunicorn/workers/base.py", line 154, in handle_abort
    sys.exit(1)
SystemExit: 1
----------------------------------------
 * Running on http://127.0.0.1:5000/
 * Running on http://127.0.0.1:5000/
 * Running on http://127.0.0.1:5000/
 * Running on http://127.0.0.1:5000/
 * Running on http://127.0.0.1:5000/

As mentioned, everything works fine if I run through Flask's native server. Issues only arise with gunicorn. Help?

Upvotes: 4

Views: 7412

Answers (1)

Miguel Grinberg
Miguel Grinberg

Reputation: 67479

I suspect your problem is that you are calling app.run().

The app.run() function starts Flask's development web server. When you use a web server other than Flask's you do not have to call this function, your web server (gunicorn in this case) will have its own way to start.

Normally the app.run() line is inside a if __name__ == '__main__': conditional (see the Flask official documentation for an example), so that it only runs when you execute the script directly, as in python run.py. I recommend that you add this to your run.py script and retest. If there are any remaining problems please describe.

Upvotes: 6

Related Questions