Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44563

Re: python process accounting

From Robert Kern <robert.kern@gmail.com>
Subject Re: python process accounting
Date 2013-04-30 19:19 +0100
References <CAOF-KfhSkQrRPjiyN3bkLwCguFw58Pzf-nu1ne9ZoZDQw3t6SQ@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1195.1367345987.3114.python-list@python.org> (permalink)

Show all headers | View raw


On 2013-04-30 17:25, Rita wrote:
> Hi,
>
> I was wondering if it possible to write a python wrapper which will account my
> processes. I would like to account for all the children processes (fork) by
> looking at their /proc/<pid> info. Such as memory, io, open files, stats.
>
> So, instead of me running "/bin/sleep 10", i would like to run it as "pywrap.py
> /bin/sleep 10" and it will do an exec /bin/sleep 10 and do a periodic snapshot
> for whats in /proc/<pid>/stats.

Something like this is approximately the right structure for such a program.


#!/usr/bin/env python

import sys
import subprocess
import time


PERIOD = 1.0  # seconds


def check_stats(pid):
     # You will have to implement this.
     # Check out psutil:
     #   https://pypi.python.org/pypi/psutil
     print 'Checking process {0}'.format(pid)


def main():
     cmd = sys.argv[1:]
     p = subprocess.Popen(cmd)
     while p.poll() is None:
         check_stats(p.pid)
         # There are more accurate ways to do this, but this probably
         # suffices for you.
         time.sleep(PERIOD)


if __name__ == '__main__':
     main()


-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: python process accounting Robert Kern <robert.kern@gmail.com> - 2013-04-30 19:19 +0100

csiph-web