[python-win32] fast test if (unknown) process is running ? Why not use Autoit ?

sharpblade sharpblade1 at gmail.com
Fri Aug 6 15:37:15 CEST 2010


You are not using the WMI efficiently. You iterate over every process to
test if only one is there, when you can use WMI like so:

>>> import wmi
>>> x = wmi.WMI()
>>> x.query("SELECT * FROM Win32_Process WHERE Name = 'xchat.exe'")
[<_wmi_object: \\TOM-PC\root\cimv2:Win32_Process.Handle="7052">]
>>> def test():
 t1 = time.time()
x.query("SELECT * FROM Win32_Process WHERE Name = 'xchat.exe'")
 print time.time()-t1

>>> import time
>>> test()
0.0829999446869
>>>

You can further reduce load times by requesting less data from WMI, i.e
stuff you are going to use:
>>> def test():
t1 = time.time()
 x.query("SELECT Handle FROM Win32_Process WHERE Name = 'xchat.exe'")
print time.time()-t1
>>> test()
0.0599999427795

On Fri, Aug 6, 2010 at 2:27 PM, Stef Mientki <stef.mientki at gmail.com> wrote:

>  thanks Tim,
>
> On 06-08-2010 13:44, Tim Golden wrote:
> > On 06/08/2010 12:27, Stef Mientki wrote:
> > <snip
>
> > Try psutil (which uses the toolhelp DLL):
> >
> > http://code.google.com/p/psutil/
> >
> > <code>
> > import psutil
> >
> > for p in psutil.process_iter ():
> >   if p.name == "blah":
> >     print p
> >     break
> > else:
> >   print "Not found"
> >
> > </code>
> >
> yes that works quit well, just a little bit slower than AutoIt:
> AutoIt: 0 .. 15 msec
> psutils: 40 ..50 msec
>
> thanks,
> Stef Mientki
> > TJG
> > _______________________________________________
> > python-win32 mailing list
> > python-win32 at python.org
> > http://mail.python.org/mailman/listinfo/python-win32
>
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20100806/cd91f2ff/attachment.html>


More information about the python-win32 mailing list