speeding up Python when using wmi

Tim Golden tim.golden at viacom-outdoor.co.uk
Mon Nov 28 09:43:44 EST 2005


[rbt]

> Here's a quick and dirty version of winver.exe written in Python:

[.. snip ..]

> It uses wmi to get OS information from Windows... it works well, but 
> it's slow... too slow. Is there any way to speed up wmi?

> In the past, I used the platform and sys modules to do some of what 
> winver.exe does and they were rather fast. However, since switching to

> wmi (for a more accurate representation) thinngs have gotten slow... 
> real slow.

> I suppose this could be a wmi only issue not related at all to Python.

In short, I recommend replacing the wmi module by the underlying
calls which it hides, and replacing Tkinter by a win32gui MessageBox.
The wmi module does some magicish things which are useful for
interactive
browsing, but will only slow you down if you know exactly what you need.
As you don't need anything more than a native message box, don't
bother with GUI loops etc. Windows will do that for you in a Modal
Dialog (here message box).

This was going to be a longer post comparing versions, but in short
running this code:

<python>
import win32gui
import win32com.client

for os in win32com.client.GetObject ("winmgmts:").InstancesOf
("Win32_OperatingSystem"):
  win32gui.MessageBox (
    0,
    os.Properties_ ("Caption").Value + "\n" + \
      os.Properties_ ("TotalVisibleMemorySize").Value + "\n" + \
      os.Properties_ ("Version").Value + "\n" + \
      os.Properties_ ("CSDVersion").Value,
    "Platform Info", 
    0
  )
</python>

is nearly as fast as running its VBS equivalent:

<vbs>
For Each os in GetObject("winmgmts:").InstancesOf
("Win32_OperatingSystem")
  
  WScript.Echo os.Caption & VbCr & _
    os.TotalVisibleMemorySize & VbCr & _
    os.Version & VbCr & _
    os.CSDVersion & VbCr

Next
</vbs>

So, if you want to use Python, you could use the script above,
but if you don't care, use VBScript, which probably has a favoured
place in the Windows World (tm).

TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________



More information about the Python-list mailing list