我有一个Flask应用程序我正试图过渡到通过gunicorn。我遇到了很多这方面的问题。这是我的应用程序的运行代码:
app.run(host=HOST, port=PORT, debug=DEBUG_FLAG)
首先,如果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
如果我用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/
如上所述,如果我运行Flask的本机服务器,一切正常。只有gunicorn会出现问题。帮助
答案 0 :(得分:4)
我怀疑你的问题是你在呼叫app.run()
。
app.run()
函数启动Flask的开发Web服务器。当您使用Flask之外的Web服务器时,您不必调用此函数,您的Web服务器(本例中为gunicorn)将有自己的启动方式。
通常app.run()
行位于if __name__ == '__main__':
条件内(有关示例,请参阅Flask官方文档),因此它只会在您直接执行脚本时运行,如python run.py
中所示。我建议您将其添加到run.py脚本并重新测试。如果还有任何遗留问题请说明。