Skip to content

Instantly share code, notes, and snippets.

@jefftriplett
Created March 1, 2013 16:35
Show Gist options
  • Select an option

  • Save jefftriplett/5065877 to your computer and use it in GitHub Desktop.

Select an option

Save jefftriplett/5065877 to your computer and use it in GitHub Desktop.
Using Django 1.5's StreamingHttpResponse
"""
Demo adapted from this stackoverflow question:
http://stackoverflow.com/questions/2922874/how-to-stream-an-httpresponse-with-django
- Updated `HttpResponse` to use `StreamingHttpResponse`
- I changed the blank spaces from 1024 to 10240 so that my browser would
update every second vs. every 4 seconds.
"""
import time
from django.http import StreamingHttpResponse
def stream_response_generator():
yield "<html><body>\n"
for x in range(1, 100):
yield "<div>%s</div>\n" % x
yield " " * 10240
time.sleep(1)
yield "</body></html>\n"
def stream_response(request):
resp = StreamingHttpResponse(stream_response_generator(), mimetype='text/html')
return resp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment