This works on my local (having Apache2) and on my Compute Engine having NGINX.
My views.py :
from django.shortcuts import render
from django.http import StreamingHttpResponse
import time
def home(request):
return render(request, 'home.html', {})
def automate_run(request):
# content_type doesn't have to be application/json
response = StreamingHttpResponse(automate_foo(), content_type="application/json")
response['Cache-Control'] = 'no-cache' # prevent client cache
response["X-Accel-Buffering"] = "no" # Allow Stream over NGINX server
return response
def automate_foo():
for i in range(1, 11):
status = "Step %d" % i
percentage = i * 10
json_string = f'{{"status": "{status}","percentage": "{percentage}"}}'
print(json_string)
yield json_string
time.sleep(.5)
But response["X-Accel-Buffering"] = "no"
doesn’t seem to have any effect on Google App Engine which I understand uses NGINX under the hood.
Demo : https://cloudxchange.el.r.appspot.com (right-click > Inspect)