I want to read in real time the output of a subprocess called from a form in a view. Here is my views.py:
@login_required
@condition(etag_func=None)
def sync_form(request):
# do things to validate form
return HttpResponse(sync_job(request, job_id, src, dest))
def sync_job(request, job_id, src, dest):
# we check the arguments here
sync_process = Popen([str(command), str(arguments), str(src), str(dest)], stdout=PIPE)
for line in sync_process.stdout:
yield simplejson.dumps(line.rstrip())
syncoutput,syncerror = sync_process.communicate()
check.log = syncoutput
check.save()
I read here that the etag function might prevent streaming, so it is best to disable. This is what I have to get the json data:
$.ajax({
url: '{% url monitor %}',
dataType: 'json',
type: 'get',
success: function(data) {
$('#sync_response').html(data);
}
});
monitor is the url of the app that has the form to submit the request. When I request the subprocess it stays in the same url (http://localhost:8000/monitor) and just gives me the plain-text output like this:
"sending incremental file list""""sent 116 bytes received 12 bytes 256.00 bytes/sec"
Is it possible to stream the data using this approach? If so, what am I doing wrong? Thanks.