Hi,
I have to execute commands such as
iostat 3 > temp.txt & on server side
Then, further send ajax request at each interval
to get updated output on the client side
When I execute this command using python subprocess
module unless I kill the process file data is not
read.
in views.py the code for servicing ajax request is as follows:
def displayoutput(request):
if request.method == 'POST':
offset = request.POST['offset']
flag = "true"
pprint (offset)
pprint (request.POST.items())
offsetint = int(offset)
chunk = 2049
filename = request.POST['filename']
g = "filename " + filename
pprint (g)
f = open(filename,'r')
pprint (f)
f.seek(offsetint)
read_data = f.read()
read_data = escape(read_data)
d = "read_data " + read_data
pprint (d)
offsetint = f.tell()
if read_data == "":
flag = "false"
b = "flag " + flag
pprint (b)
return_dict = {'filedata':read_data,'offset':offsetint,'flag':flag}
json = simplejson.dumps(return_dict)
return HttpResponse(json,mimetype="application/json")
In my template the code is as follows :
<html>
<head>
<script type="text/javascript" src="/jquerycall/"></script>
<script type="text/javascript">
var setid = 0;
var s = new String();
var my_array = new Array();
var no_of_lines = 0;
var array_copy = new Array();
var fname ="{{fname}}"
var offset = 0
function displayfile()
{
$.ajax({
type:"POST",
url:"/displayoutput/",
datatype:"json",
data:{offset:offset,filename:fname},
success:function(data)
{
var s = data.filedata;
/*alert(s)*/
var flag = data.flag;
offset = data.offset;
if(flag == 'false')
{
clearInterval(setid);
}
my_array = s.split("\n");
displaydata(my_array);
/*$('#textid').append(s)*/
}
});
$("#textid").append(offset)
}
function displaydata(my_array)
{
var i = 0;
var length = my_array.length;
for(i=0;i<my_array.length;i++)
{
var line = my_array[i] + "\n";
$("#textid").append(line);
}
}
$(document).ready(function()
{
setid = setInterval(displayfile,2000);
});
</script>
</head>
<body>
<form method="post">
<textarea id="textid" disabled="true" readonly="true" rows=30 cols=100></textarea><br><br>
<!-- No Of Lines<input type="text" name="text" id="tid"> -->
<!-- <input type="submit" name="showdate" value="start" id="startid"/> -->
</form>
</body>
</html>