Created
March 1, 2013 16:35
-
-
Save jefftriplett/5065877 to your computer and use it in GitHub Desktop.
Using Django 1.5's StreamingHttpResponse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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