I have not read everything carefully yet .I am doing a system monitor ajax+web2py application , which monitor CPU and MEM at the same time and updates the page with jquery ajax effects. its pretty simple.So it is multi-threading in a webapp . to get response from multiple httprequest at the same time can be done like this.
NOTE: for this System monitor to work, Requires libstatgrab and pystatgrab .
libstatgrab + pystatgrab works under Linux and Cygwin.
to
in controllers , default.py :import statgrab
def index():
return dict()
def cpumon():
cpustat = statgrab.sg_get_cpu_percents()
perc = int(round(100 - cpustat['idle']))
return perc
def memon():
#otno=random.randrange(0,100)
memstat = statgrab.sg_get_mem_stats()
memdiff = memstat['total'] - (memstat['cache'] + memstat['free'] )
memfloat = float (memdiff) / float(memstat['total'])
perc = int(round (memfloat * 100))
return perc
in view . Index.html :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"
http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>Ajax + Web2py monitor</title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<script type="text/javascript" src="js/jq.js"/>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="author" content="Your name here" />
<meta name="Copyright" content="Copyright (c) Your copyright here 2005" />
<script language="JavaScript1.8">
// You can put Functions i
</script>
<script language="JavaScript1.8">
var clockid = 0;
function updatecpu()
{
if(clockid)
{
clearTimeout(clockid);
clockid = 0;
}
cpueffect();
clockid = setTimeout('updatecpu()',2000);
}
function cpueffect() {
$.post(('cpumon'),function(data)
{
if (data < 6){
$("#amount").animate(
{
width: 6 + "%"
},1000);
document.getElementById('amount').innerHTML=data + '%';
} else {
$("#amount").animate(
{
width: data + "%"
},1000);
document.getElementById('amount').innerHTML=data + '%';
}
});
}
$(document).ready(updatecpu());
</script>
<script language="JavaScript1.8">
var clockid2 = 0;
function updatemem()
{
if(clockid2)
{
clearTimeout(clockid2);
clockid2 = 0;
}
memeffect();
clockid2 = setTimeout('updatemem()',2000);
}
function memeffect()
{
$.post(('memon'),function(data)
{
$("#mem").animate(
{
width: data + "%"
},1000);
document.getElementById('mem').innerHTML=data + '%';
});
}
$(document).ready(updatemem());
</script>
</head>
<body>
<h5><a href="#" onclick=blinds() >JxMon </a></h5>
<!-- ****************** BEGIN CPU Progress Bars ****************** -->
<h3><a href="#" onclick=blinds() >cpu: </a></h3>
<div class= "mon_cont" >
<div class="cpumon_fill" id='amount'></div>
</div>
<h3><a href="#" onclick=blinds() >mem: </a></h3>
<div class= "mon_cont" >
<div class="memmon_fill" id='mem'></div>
</div>
</body>
</html>