How to stream an HttpResponse with Django

muudscope :

I'm trying to get the 'hello world' of streaming responses working for Django (1.2). I figured out how to use a generator and the yield function. But the response still not streaming. I suspect there's a middleware that's mucking with it -- maybe ETAG calculator? But I'm not sure how to disable it. Can somebody please help?

Here's the "hello world" of streaming that I have so far:

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

def stream_response_generator():
    for x in range(1,11):
        yield "%s\n" % x  # Returns a chunk of the response to the browser
        time.sleep(1)
Leopd :

You can disable the ETAG middleware using the condition decorator. That will get your response to stream back over HTTP. You can confirm this with a command-line tool like curl. But it probably won't be enough to get your browser to show the response as it streams. To encourage the browser to show the response as it streams, you can push a bunch of whitespace down the pipe to force its buffers to fill. Example follows:

from django.views.decorators.http import condition

@condition(etag_func=None)
def stream_response(request):
    resp = HttpResponse( stream_response_generator(), content_type='text/html')
    return resp

def stream_response_generator():
    yield "<html><body>\n"
    for x in range(1,11):
        yield "<div>%s</div>\n" % x
        yield " " * 1024  # Encourage browser to render incrementally
        time.sleep(1)
    yield "</body></html>\n"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How read a file and stuff it into a HttpResponse in django?

How to return a django queryset in ajax httpresponse

How to disable Stream in Django

Does a file served through Django HttpResponse enter memory or stream from disk?

How to get a string from HttpResponse or render to a string in Python/Django?

how to get django to return both an HttpResponse AND render a different template

How to get and use HTTPResponse message from a DJANGO server

Django: How to return an attribute inside an HttpResponse with meta tags?

how can I output a variable to a django template via HttpResponse?

Akka Stream use HttpResponse in Flow

Django View not returning httpresponse

django-HttpResponse returned None

Django Tutorial: name 'HttpResponse' is not defined

HTTPS equivalent of Django's HttpResponse

Django return image and data in HttpResponse

Django - display Json or Httpresponse in template

Ajax not appending HttpResponse from Django

How to read the HttpResponse Body?

How to download an HttpResponse into a file?

How to transform a HttpResponse into a HttpErrorResponse

How to fix Django's the view didn't return an HttpResponse object. It returned None instead?

How to fix Django : didn't return an HttpResponse object. It returned None instead?

How to fix a Django view that is not returning an HttpResponse Object? (CS50 Project 1)

How to configure stream-django with DRF

Django file field how to fill with stream io

How to stream opencv frame with django frame in realtime?

How to add header in mock HTTPResponse

How to write zip file into httpresponse

How to Log HttpRequest and HttpResponse in a file?