Gunicorn在Flask应用上不断重启/崩溃
我有一个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时才会出现问题。谁能帮帮我?
1 个回答
6
我觉得你的问题可能是因为你在调用 app.run()
。
app.run()
这个函数是用来启动 Flask 的开发用网页服务器的。当你使用其他网页服务器(比如这里的 gunicorn)时,就不需要调用这个函数,因为你的网页服务器有自己的启动方式。
通常情况下,app.run()
这一行代码会放在 if __name__ == '__main__':
这个条件语句里面(你可以查看 Flask 的官方文档来了解示例),这样只有在你直接运行这个脚本时,比如用 python run.py
,它才会执行。我建议你把这个加到你的 run.py 脚本里,然后再测试一下。如果还有其他问题,请详细描述一下。