views:

131

answers:

3

Is there an easy way to find out the current (real or cpu) run time of a subprocess.Popen instance?

A: 

Not in a platform-independent manner. On Linux, you can read /proc/<pid>/stat, in particular the columns utime, stime, and starttime (as described in proc(5)).

Martin v. Löwis
+1  A: 

No, but you can simply subclass and extend the Popen class to store the time it was created.

THC4k
A: 

On a Windows machine, you can use win32 APIs along with proc.pid, like so:

import subprocess

def print_times(proc):
    import win32process, win32api, win32con
    hproc = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION, False, proc.pid)
    times = win32process.GetProcessTimes(hproc)
    win32api.CloseHandle(hproc)
    # GetProcessTimes returns values in 100-ns intervals
    print 'kernel', times['KernelTime'] * 1e-7
    print 'user', times['UserTime'] * 1e-7

proc = subprocess.Popen('cmd /c sleep 1')
proc.wait()
print_times(proc)
Paul Du Bois