Django 1.5 - 使用新的StreamingHttpResponse [英] Django 1.5 - using the new StreamingHttpResponse

查看:159
本文介绍了Django 1.5 - 使用新的StreamingHttpResponse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我实现 StreamingHttpResponse 如此处所示,直到10秒钟之后,流响应才会显示。 djangoproject 的信息不多,除了说这对于生成大型CSV文件很有用,同时警告昂贵的任务应该在请求 - 响应周期之外执行。

If I implement StreamingHttpResponse as shown here, the 'streaming' response is not shown until the 10 seconds is up. There isn't much information on djangoproject except saying it's useful for generating large CSV files while warning that expensive tasks should be performed outside of the request-response cycle.

但是,我看不到它正在工作使用时间密集的代码。有什么关于发生器对象阻止这个?以下是代码重复供参考。

However, I cannot see that it is working at all using time-intensive code. Is there something about the generator object that prevents this? Here is the code duplicated for reference.

import time
from django.http import StreamingHttpResponse

def stream_response(request):
    resp = StreamingHttpResponse(stream_response_generator())
    return resp

def stream_response_generator():
    for x in range(1,11):
        yield '{} <br />\n'.format(x)
        time.sleep(1)


推荐答案

[OP的解决方案转换为以下答案]

[OP's solution converted to answer below]

Pavel的评论指出我的例子的问题是浏览器的缓冲,这是通过修改发送的数据量来解决的,例如

Pavel's comment pointed out that the problem with my example was with the browser's buffering, which is solved by modifying the amount of data sent, as e.g.

yield '{} <br /> {}'.format(x, ' '*1024)

这篇关于Django 1.5 - 使用新的StreamingHttpResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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