In python, how do I get timing information like os.times() for a specific child process?
Question: I can use os.times or resource.getrlimit to get user time of the current
process, all the children processes, or both.

How do I get timing information for a specific child process?


Answer: I don't know if you can do this in general, but on Linux you can just parse
`/proc/<PID>/stat` to find the first four values that you would get from
`os.times` for any process. For example:

    
    
    import sys
    import os
    
    if not len(sys.argv) == 2:
        print >> sys.stderr, "Usage: %s <PID>" % (sys.argv[0])
        sys.exit(1)
    
    pid = int(sys.argv[1])
    
    hz = os.sysconf(os.sysconf_names['SC_CLK_TCK'])
    
    with open("/proc/%d/stat" % (pid,)) as fp:
        fields = fp.read().split(' ')[13:17]
        utime, stime, cutime, cstime = [ (float(f) / hz) for f in fields ]
        print "utime in clock ticks:", utime
        print "stime in clock ticks:", stime
        print "cutime in clock ticks:", cutime
        print "cstime in clock ticks:", cstime
    

Presumably you have the process IDs of the child processes you're interested
in.

I found out how to get the value of `sysconf(_SC_CLK_TCK)` from this question:
[Python: How to get number of mili seconds per
jiffy](http://stackoverflow.com/questions/4189123/python-how-to-get-number-of-
mili-seconds-per-jiffy/4189612#4189612) and the information about the fields
in `/proc/<PID>/stat` is from the `proc(5)` man page.

