Measure cpu usage of child but not grandchildren

1.2k views Asked by At

This might be an obvious thing, but I can't seem to figure out how to do it.

Assuming that I spawn a process like this:

    popen = subprocess.Popen(args, executable=executable,
                                  bufsize=-1,
                                  stdin=subprocess.PIPE,
                                  stdout=subprocess.PIPE,
                                  close_fds=True,
                                  env={})
    popen.wait()

Is there any way I can measure the cpu usage of this child, but not the grandchildren?

   resource.getrusage(resource.RUSAGE_CHILDREN)

Gives me all children and granchildren.

1

There are 1 answers

0
Mihai Stan On

You can use psutil

import psutil
popen = ...
p = psutil.Process(popen.pid)
print p.get_cpu_times()