[Django] Pass iterator to response
Sometimes it may be more efficient to transmit response while it being generated.
Before use this, notice possible performance issues:
- Django is designed for short-lived requests. Streaming responses will tie a worker process for the entire duration of the response. This may result in poor performance.
- Generally speaking, you should perform expensive tasks outside of the request-response cycle, rather than resorting to a streamed response.
def my_iterator(): yield 'Hi!\n' for i in MyModel.objects.all(): yield '%s\n' % i.field yield 'By!\n' def my_view(request): response = HttpResponse(my_iterator(), content_type='text/plain') response['Content-Disposition'] = 'attachment; filename=sometext.txt' return response
Licensed under CC BY-SA 3.0