8

I've got a view like the following:

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')

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?

synic
  • 26,359
  • 20
  • 111
  • 149
  • 1
    related: http://stackoverflow.com/questions/2922874/how-to-stream-an-httpresponse-with-django – miku May 28 '10 at 22:36
  • Yeah, that's where I got the code ideas to try, however, it doesn't seem to be working for me. Not sure what I'm doing wrong here. – synic May 28 '10 at 22:47
  • What you're doing is completely not standard and only a miracle that occurs because of the details of how some browser implement http. I'd say, try it with a bunch of browsers and varying amounts of whitespace.. Eventually, you'll probably find a remotely exploitable security hole. –  Jun 04 '10 at 07:58

1 Answers1

3

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.

Tobu
  • 24,771
  • 4
  • 91
  • 98