【问题标题】:Gunicorn Keeps Restarting/Breaking on Flask AppGunicorn 在 Flask 应用程序上不断重启/中断
【发布时间】:2014-10-22 14:12:46
【问题描述】:

我有一个 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 才会出现问题。帮忙?

【问题讨论】:

  • 有 gunicorn 配置/命令行吗?与此比较:flask.pocoo.org/docs/0.10/deploying/wsgi-standalone
  • 无特殊配置。从gunicorn analytics_service:app 开始。现在所有的配置都是通过 Flask 完成的,只需要设置主机、端口和 debug_flag。

标签: python python-2.7 flask gunicorn


【解决方案1】:

我怀疑你的问题是你打电话给app.run()

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

通常app.run() 行位于if __name__ == '__main__': 条件内(请参阅Flask 官方文档以获取示例),因此它仅在您直接执行脚本时运行,如python run.py。我建议您将其添加到您的 run.py 脚本并重新测试。如果还有其他问题,请描述。

【讨论】:

  • 是否有关于烧瓶和 gunicorn 应该如何交互的文档?我无法通过猜测让它工作,我在项目根目录有一个简单的 run.py,它导入 projectname/index.py 并调用其run 方法,此方法创建 Flask 应用程序并调用app.run(),没有这个技巧导入无法按预期工作,因为无法从其他文件所依赖的项目根目录中找到“projectname”模块。
  • Flask 在 gunicorn 方面没有什么特别之处。它只是一个 WSGI 应用程序,因此您可以按照 gunicorn 文档并使用 Flask 应用程序实例作为您的 WSGI 应用程序。
  • 啊哈,我在看flask.pocoo.org/docs/0.10/deploying/wsgi-standalone,但docs.gunicorn.org/en/latest/run.html 在找到正确的语法和设置方面更有用。
猜你喜欢
  • 2021-01-15
  • 1970-01-01
  • 2022-09-23
  • 2018-07-08
  • 2016-05-03
  • 1970-01-01
  • 2021-02-16
  • 2015-12-23
  • 2021-08-29
相关资源
最近更新 更多