django以stream的方式返回请求

views.py
html

@condition(etag_func=None)
def start_brecc_import(request):
    resp = HttpResponse( stream_generator(), mimetype='text/html')
    return resp
def stream_generator():
    yield "<html><body>\n"
    for k in range(10):
        yield k
        yield " " * 1024 # force browser to output
        time.sleep(1)
    yield "<p>complete!!</p><a href=\"/\">return</a></body></html>\n"
  • 使用condition修饰符修饰对应的view函数。python

  • 使用一个产生器(generator)函数不断的发出流数据浏览器

  • 因为浏览器自己会对http响应作buffering,为了能在浏览器中看到stream的信息一条一条的输出,则须要增长yield " " * 1024强制填充缓冲区。若是只是用命令行的curl之类的去读取,则能够不须要这句话curl

相关文章
相关标签/搜索