Gunicorn 在 Flask 应用程序上不断重启/中断

Eli*_*Eli 4 python flask python-2.7 gunicorn

我有一个 Flask 应用程序,我正在尝试过渡到通过 gunicorn 运行。我在这方面遇到了很多问题。这是我的应用程序的运行代码:

app.run(host=HOST, port=PORT, debug=DEBUG_FLAG)
Run Code Online (Sandbox Code Playgroud)

首先,如果 DEBUG_FLAG == true,应用程序将永远不会真正启动,而是会继续重启,并且在本地点击它是行不通的。它只是一遍又一遍地这样做:

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
Run Code Online (Sandbox Code Playgroud)

如果我用 DEBUG_FLAG==False 启动它,它实际上会启动并处理一些请求,但仍会因未知原因频繁中断和重启:

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/
Run Code Online (Sandbox Code Playgroud)

如前所述,如果我通过 Flask 的本机服务器运行,一切正常。问题只出现在 gunicorn 上。帮助?

Mig*_*uel 5

我怀疑您的问题是您正在调用app.run().

app.run()函数启动 Flask 的开发 Web 服务器。当您使用 Flask 以外的 Web 服务器时,您不必调用此函数,您的 Web 服务器(在本例中为 gunicorn)将有自己的启动方式。

通常,该app.run()行位于if __name__ == '__main__':条件语句中(示例参见 Flask 官方文档),因此它仅在您直接执行脚本时运行,如python run.py. 我建议您将其添加到 run.py 脚本中并重新测试。如果有任何遗留问题,请描述。