ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python-Gunicorn在Flask App上不断重启/中断

2019-10-29 06:57:56  阅读:802  来源: 互联网

标签:python-2-7 flask gunicorn python


我有一个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脚本并重新测试.如果还有其他问题,请描述.

标签:python-2-7,flask,gunicorn,python
来源: https://codeday.me/bug/20191029/1958199.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有