我有一个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的本机服务器运行一切正常.问题只出现在金枪鱼上.救命?
解决方法:
我怀疑您的问题是您正在调用app.run().
app.run()函数启动Flask的开发Web服务器.当您使用Flask以外的Web服务器时,不必调用此功能,您的Web服务器(本例中为gunicorn)将有其自己的启动方式.
通常,app.run()行位于if __name__ ==’__main__’内部:有条件的(请参见Flask官方文档中的示例),因此仅当您直接执行脚本时才运行它,例如pythonrun.py.我建议您将其添加到run.py脚本并重新测试.如果还有其他问题,请描述.