How to check on process time?

Donn Cave donn at u.washington.edu
Wed Jan 10 16:04:32 EST 2001


Quoth noahspurrier at my-deja.com:
| Which Python library do I want to get extensive process information?
| I need to look at any process running -- not just the parent, current,
| or child processes.

As far as I know, that isn't supported on contemporary computer
platforms, at least in any meaningfully portable way, and that
would be why you don't see anything for it in Python.  The "ps"
command doesn't use any kind of API, it just reads kernel memory
where this stuff is stored.

If it doesn't need to be portable, and your present platform
supports it, I'd say use the "/proc" filesystem.  The following
prints accumulated times, on FreeBSD 4.1.

	Donn Cave, donn at u.washington.edu
----------------------------------------
import errno
import posix
import string

def ps():
        proc = posix.listdir('/proc')
        for pid in proc:
                try:
                        pidval = string.atoi(pid)
                except ValueError:
			#  Ignore /proc/curproc etc.
                        continue
                try:
                        fp = open('/proc/%s/status' % (pid,), 'r')
                except IOError, val:
                        if val.errno == errno.ENOENT:
                                print pid, '... already gone'
                                continue
                        else:
                                raise
                ln = fp.readline()
                fp.close()
                ln = string.split(ln)
                cmd = ln[0]
                uss, usu = map(string.atoi, string.split(ln[8], ','))
                sys, syu = map(string.atoi, string.split(ln[9], ','))
                print '%5s %-14s%6d.%06d %d.%09d' % (pid, cmd, uss, usu, sys, syu)

ps()



More information about the Python-list mailing list