8

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)
Community
  • 1
  • 1
Frank
  • 899
  • 1
  • 8
  • 13
  • 8
    Your 100 bytes, streaming or not, will absolutely be buffered at the lowest levels of network. Try a million rows for a start. – Pavel Anossov Mar 12 '13 at 11:26
  • @Pavel Thanks for that - I added ' '*1024 to the yield string and it works as desired – Frank Mar 12 '13 at 12:17

1 Answers1

2

[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)
jam
  • 3,640
  • 5
  • 34
  • 50