[python-win32] win32api documentation

Tim Golden mail at timgolden.me.uk
Mon Oct 12 18:13:11 CEST 2009


J wrote:
> Ok... that all helped a lot.  I've been playing around with memory a
> bit, and cache, and I'll poke at that some more later.  Today I've
> been looking into processors.
> 
> So WMI provides Win32_Processor and from what I've read, there exists
> one instance of Win32_Processor for each processor on a SMP machine.
> 
> So for starters, I assume then that if I had one such machine, I could
> count all the cores simply by doing something like:
> 
> for p in c.Win32_Processor :
>     procCount = procCount + 1
> 
> which makes sense.

Ummm. Not quite.

c.Win32_Processor is a class-ish thing which you have to call to
pull back the *list* of instances. Which you can then just
count. So something like this:

n_processors = len (c.Win32_Processor ())

I can't at this moment remember how the c.Win32_Processor interacts
with physical processors, multicore processors and hyperthreading.
It's probably documented somewhere, and a little experimentation
should give you answers quite easily. That's the great thing with
Python: you just fire up the interpreter and get the answer!

> Now that I look at it, I seem to be able to pull information based on
> each core, and I can count cores.  But what about counting processor
> sockets or packages?  For example,
> 
> Lets say, I do that simple loop and get a return of 16.
> 
> How do I differentiate between 16 cores, or 8 cores with
> HyperThreading turned on?
> 
> Admittedly, that's more an educational curiosity.  The systems I'll be
> using all this on are readily accessible, and we'll know ahead of time
> that System A has 2 4-core processors and that we turned HT off in
> BIOS, so we should only see 8 logical processors when we test.  Any
> delta from that will fail the test.
> 
> So like I said, I was just curious if there is any built in way via
> WMI or whatever to differentiate without having to go and manually
> create a class based on the Windows API (because I'm not good enough
> to do that kind of work).

This is definitely the place to look for this kind of information:

http://msdn.microsoft.com/en-us/library/aa394373%28VS.85%29.aspx

unfortunately, some of the useful information like NumberOfCores
and NumberOfLogicalProcessors seems to be available only on the
very latest releases. Might be an issue for you?


> 
> Either way, thanks for the help so far, the WMI wrapper is pretty
> cool.  I've never written system level code before, beyond
> instrumenting some device drivers and services to see why they break,
> so this has been pretty fun so far as a learning experience.

Glad it's been useful. I'm just releasing a new version of the
Python module which includes a little web server whichi lets you
browse around the classes to a limited extent. If you're interested,
let me know.

TJG


More information about the python-win32 mailing list