speeding up Python when using wmi

8 views
Skip to first unread message

rbt

unread,
Nov 28, 2005, 8:40:07 AM11/28/05
to
Here's a quick and dirty version of winver.exe written in Python:

http://filebox.vt.edu/users/rtilley/public/winver/winver.html

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.

Any tips or ideas?

Thanks!

Tim Golden

unread,
Nov 28, 2005, 9:43:44 AM11/28/05
to pytho...@python.org
[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
________________________________________________________________________

rbt

unread,
Nov 28, 2005, 10:21:08 AM11/28/05
to

Wow... thanks. I didn't expect someone to completely rewrite it like
that. I'll use your example and name it PyWinver and attribute it to
you. Hope you don't mind. Great learning experience.

Thanks!

Tim Golden

unread,
Nov 28, 2005, 10:30:27 AM11/28/05
to pytho...@python.org
[rbt]
>> Here's a quick and dirty version of winver.exe written in Python:

[Tim Golden]


> In short, I recommend replacing the wmi module by the underlying
> calls which it hides, and replacing Tkinter by a win32gui MessageBox.

[rbt]


>Wow... thanks. I didn't expect someone to completely rewrite it like
>that. I'll use your example and name it PyWinver and attribute it to
>you. Hope you don't mind. Great learning experience.
>Thanks!

My pleasure. It wasn't that much of a rewrite; there were
only, what, a dozen lines in the original. One of those
rare occasions when I have to *not* recommend using the
wmi module!

Reply all
Reply to author
Forward
0 new messages