流式HTTP响应,冲洗到浏览器 [英] Streaming HTTP response, flushing to the browser

查看:478
本文介绍了流式HTTP响应,冲洗到浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的视图:

from django.views.decorators.http import condition

def stream():
    for i in range(0, 40):
        yield " " * 1024
        yield "%d" % i
        time.sleep(1)

@condition(etag_func=None):
def view(request):
    return HttpResponse(stream(), mimetype='text/html')

然而,这绝对不是流媒体。所有的数据都是一次倾倒,最后在大约40秒之后。如何使其正常清理?

However, it definitely doesn't seem to be streaming at all. All the data is dumped at once, at the end, after about 40 seconds. How can I get it to flush correctly?

推荐答案

要使http流工作,您的中间件WSGI容器和Web服务器以及反向代理/负载均衡器,如果您使用nginx + apache2 syle部署)都必须支持它。禁用中间件并删除一些图层,直到它工作(任何缓存或设置etags的东西都已经出来),然后将它们添加回来。我不知道你有什么堆栈,但是IIRC apache2 + mod_python可以使用流式传输,但这不是推荐的Django服务。

To make http streaming work, your middleware, WSGI container, and web server (as well as the reverse-proxy/load-balancer if you use an nginx+apache2 syle deployment) all have to support it. Disable the middleware and remove some of the layers until it works (anything that does caching or sets etags is out), then add them back. I don't know what stack you have, but IIRC apache2 + mod_python is OK with streaming, though it's not the recommended way to serve Django.

这篇关于流式HTTP响应,冲洗到浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆