Skip to content

Instantly share code, notes, and snippets.

@gbrayut gbrayut/testwmi.py
Last active Jan 6, 2017

Embed
What would you like to do?
Python testing WMI
#pip install comtypes wmi
import sys, os, traceback
sys.coinit_flags = 0 # sets pythoncom.COINIT_MULTITHREADED = 0
import comtypes.client # http://starship.python.net/crew/theller/comtypes/
from comtypes.automation import IDispatch, IEnumVARIANT
import wmi
#Use help(...) and dir(...) on any object/class to see more details
#These are already set on the main thread by sys package, but need to call if we use other threads
#comtypes.CoInitializeEx(comtypes.COINIT_MULTITHREADED)
def GetMemoryUsageMB():
w = wmi.WMI('.')
result = w.query("SELECT WorkingSetPrivate FROM Win32_PerfRawData_PerfProc_Process WHERE IDProcess="+str(os.getpid()))
subset = result[0]
return float(subset.WorkingSetPrivate)/1024./1024.
print "Start %.2fMB" % GetMemoryUsageMB()
progid = "WbemScripting.SWbemLocator"
clsid = comtypes.GUID.from_progid(progid)
clsctx = comtypes.CLSCTX_INPROC_SERVER | comtypes.CLSCTX_LOCAL_SERVER | comtypes.CLSCTX_REMOTE_SERVER #https://github.com/go-ole/go-ole/blob/7dfdcf409020452e29b4babcbb22f984d2aa308a/constants.go#L11
try:
i = 0
while i < 100000:
i += 1
if (i % 1000 == 0):
print "%7d %.1fMB" % (i,GetMemoryUsageMB())
comtypes.CoInitializeEx(comtypes.COINIT_MULTITHREADED) #https://github.com/enthought/comtypes/blob/d3fbdd0c2933c9671e80d7407a0320925abc8bc5/comtypes/__init__.py#L156
#com_object = comtypes.client.CreateObject(progid) #https://github.com/enthought/comtypes/blob/d3fbdd0c2933c9671e80d7407a0320925abc8bc5/comtypes/client/__init__.py#L205
com_object = comtypes.CoCreateInstance(clsid, clsctx=clsctx, interface=None) # https://github.com/enthought/comtypes/blob/d3fbdd0c2933c9671e80d7407a0320925abc8bc5/comtypes/__init__.py#L1215
iface = com_object.QueryInterface(comtypes.automation.IDispatch)
#iface.Invoke() #https://github.com/enthought/comtypes/blob/d3fbdd0c2933c9671e80d7407a0320925abc8bc5/comtypes/automation.py#L732
del iface #calls release https://github.com/enthought/comtypes/blob/d3fbdd0c2933c9671e80d7407a0320925abc8bc5/comtypes/__init__.py#L905
del com_object
comtypes.CoUninitialize()
except Exception, e:
print 'Exception: '+ str(e)
print traceback.format_exc()
'''
for key in dir(iface):
method = getattr(iface,key)
if str(type(method)) == "<type 'instance'>":
print key
for sub_method in dir(method):
if not sub_method.startswith("_") and not "clsid" in sub_method.lower():
print "\t"+sub_method
else:
print "\t",key,'=',method
'''
print "End %.2fMB" % GetMemoryUsageMB()
#comtypes calls CoUninitialize at shutdown https://github.com/enthought/comtypes/blob/d3fbdd0c2933c9671e80d7407a0320925abc8bc5/comtypes/__init__.py#L175
#comtypes.CoUninitialize()
@gbrayut

This comment has been minimized.

Copy link
Owner Author

gbrayut commented Jan 5, 2017

Output on a Windows 10 system:

python testwmi.py
Start 10.92MB  
   1000 11.5MB 
   2000 11.6MB 
   3000 11.6MB 
   4000 11.6MB 
   5000 11.6MB 
 ... omitted ...
  98000 11.8MB 
  99000 11.8MB 
 100000 11.8MB 
End 11.83MB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.