Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.5 - using the new StreamingHttpResponse

If I implement StreamingHttpResponse as shown here, the 'streaming' response is not shown until the 10 seconds is up. There isn't much information on djangoproject except saying it's useful for generating large CSV files while warning that expensive tasks should be performed outside of the request-response cycle.

However, I cannot see that it is working at all using time-intensive code. Is there something about the generator object that prevents this? Here is the code duplicated for reference.

import time
from django.http import StreamingHttpResponse

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

def stream_response_generator():
    for x in range(1,11):
        yield '{} <br />\n'.format(x)
        time.sleep(1)
like image 330
Frank Avatar asked Mar 12 '13 11:03

Frank


1 Answers

[OP's solution converted to answer below]

Pavel's comment pointed out that the problem with my example was with the browser's buffering, which is solved by modifying the amount of data sent, as e.g.

yield '{} <br /> {}'.format(x, ' '*1024)
like image 172
jam Avatar answered Sep 23 '22 07:09

jam