Process details

Quinn Dunkan quinn at riyal.ugcs.caltech.edu
Thu Mar 15 15:14:07 EST 2001


On Thu, 15 Mar 2001 09:40:38 -0800, Timothy Grant <tjg at exceptionalminds.com>
wrote:
>...I just went and looked at my code and discovered the major
>bottleneck--I think. The problem comes due to the fact that not
>all process list all entries in the /status file. This means
>that a sequential read assigning to variables won't work, so I
>am actually looking for entry names in a loop.
>
>I just added a break statement and shaved three seconds of the
>time! I'm sure there is more room to optimize, but exiting my
>loop after I found what I was looking for sure helps.

Not that you asked or anything <wink>, but the 'stat' file has mostly the same
info and is more machine-readable.  You should be able to do something like:

class Proc:
    def __init__(self, stats):
        self.pid = int(stats[0])
        self.name = stats[1] # strip parens if swapped out
        self.state = stats[2]
        # etc.

procs = [ Proc(open('/proc/%s/stat' % s).read().split())
    for s in os.listdir('/proc') if s.isdigit() ]

for p in procs:
    print p.pid, p.name, p.state



More information about the Python-list mailing list