Flask,Gunicorn,Nginx :: IOError:[Errno 32]管道破裂

时间:2018-01-15 22:28:41

标签: python nginx flask gunicorn

我正在尝试将项目从开发转移到生产。 (在开发阶段,我只使用Flask,现在我使用GunicornNginx后面运行它。)

我在服务特定网页songs.html时遇到问题。

页面正确加载虚拟变量(jukebox = [whatever]),但在现实生活中我使用的是生成器,如下所示:

playlist= query_playlist(p)
jukebox = next(playlist)

return render_template('songs.html',
                        jukebox=jukebox)

并且此函数需要一段时间(比如2s)才能返回结果...但是没有提供结果,并且只有在返回结果后进程才会挂起。

我像这样运行应用程序:

(appenv)$gunicorn -c gconfig.py app:app

wsgi.ppy

from app import app as application

if __name__ == "__main__":
    application.run(host='0.0.0.0')

gconfig.py:

workers = 16
worker_class = 'sync'
worker_connections = 1000
timeout = 120 # changed this from 30 up
keepalive = 2

nginx.conf brew安装)

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
        proxy_pass         http://127.0.0.1:8000/;
        proxy_redirect     off;

        proxy_set_header   Host                 $host;
        proxy_set_header   X-Real-IP            $remote_addr;
        proxy_set_header   X-Forwarded-For      $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto    $scheme;
        }

如果我使用python app.py运行应用,请返回开发阶段:

if __name__ == '__main__':
    app.run(use_reloader=True, threaded=True, debug=True)

至少我得到以下追溯:

Error on request:
Traceback (most recent call last):
  File "/Users/me/Documents/Code/Apps/app/appenv/lib/python2.7/site-packages/werkzeug/serving.py", line 270, in run_wsgi
    execute(self.server.app)
  File "/Users/me/Documents/Code/Apps/app/appenv/lib/python2.7/site-packages/werkzeug/serving.py", line 261, in execute
    write(data)
  File "/Users/me/Documents/Code/Apps/app/appenv/lib/python2.7/site-packages/werkzeug/serving.py", line 236, in write
    self.send_header('Server', self.version_string())
  File "/Users/me/anaconda2/lib/python2.7/BaseHTTPServer.py", line 412, in send_header
    self.wfile.write("%s: %s\r\n" % (keyword, value))
IOError: [Errno 32] Broken pipe

所以有人知道如何解决这个问题吗?

修改

即使结果需要处理2秒,我也会获得相同的追溯。

1 个答案:

答案 0 :(得分:0)

根据Flask docs(以及#arielnmz sugestion),您可以直播内容:

from flask import Response, stream_with_context

@app.route('/playlist')
def generate_playlist():
    def generate():
        jukebox = query_playlist(p)
        for i in jukebox:
            yield i[0]['artist'] 
    return Response(stream_with_context(generate()))

每个yield表达式都直接发送到浏览器。

诀窍是有一个内部函数,它使用生成器生成数据,然后调用该函数并将其传递给响应对象。

这很有效。