Get the load at a point in time or getloadavg () for a shorter period of time in Python on Linux (CentOS)

I am currently using Python os.getloadavg()to get an idea of ​​the current server load(Centos 6.3)

According to python documentation os.getloadavg()"Returns the number of processes in the system execution queue averaged over the last 1, 5 and 15 minutes":

http://docs.python.org/2/library/os.html#os.getloadavg

os.getloadavg ()
Return the number of processes in the system run queue averaged over the last 1, 5, and 15 minutes or raises OSError if the load average was unobtainable.

Question:

  • Is it possible to get the number of processes in the system execution queue? at the moment?
  • If not, is it possible to get the average for a shorter period of time, like the last 5 or 10 seconds?

The reason I ask is because I get the average load value, and then if it is too large, I kill some processes. This can happen many times per minute, so I think that too many processes will be killed before the 1 minute average overtakes.

Thank!

+5
source share
1 answer

According to Documentation / filesystems / proc.txt in the Linux 3.5 kernel source, you can get the number of currently running processes with /proc/stat:

>>> for l in open("/proc/stat"):
...   l = l.split()
...   if l[0] == 'procs_running':
...     result = int(l[1])
... 
>>> print result
6
>>> 

The same number is available in /proc/loadavg:

>>> print int(open("/proc/loadavg").next().split()[3].split('/')[0])
6
+4
source

All Articles