Question

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

Was it helpful?

Solution

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

OTHER TIPS

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)).

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top