[python-win32] How to determine if a process (known pid) is still running ?

Tim Golden mail at timgolden.me.uk
Sun Oct 5 11:03:49 CEST 2008


Stef Mientki wrote:
> How to determine if a process (known pid) os still running ?
> 
> Googling the web, I found 1 solution in killing the process,
> but that's not what I want, I want to reuse the running process.
> 
> I could also ask a complete list of active processes,
> but from my experiences that's quite slow.
> 
> Any better solutions ?

I'm not entirely sure what you're trying to achieve,
or what "reuse the running process" means. This WMI
snippet will tell you whether a given pid is running
or not. As you may know, WMI isn't the fastest thing
on earth, but it may be fast enough for you.

<code>
import wmi

pid = int (raw_input ("Enter PID:"))

c = wmi.WMI (find_classes=False)
for process in c.Win32_Process ([], ProcessId=pid):
   print "PID %s in use by %s" % (pid, process.Caption)
   break
else:
   print "PID %d not in use" % pid

</code>


TJG


More information about the python-win32 mailing list