Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streaming HTTP response, flushing to the browser

Tags:

django

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?

like image 470
synic Avatar asked May 28 '10 22:05

synic


1 Answers

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.

like image 82
Tobu Avatar answered Nov 07 '22 21:11

Tobu