cloudbase / PyMI

A blazing fast replacement for the Python WMI module
Apache License 2.0
21 stars 14 forks source link

Crashes or errors when trying examples from the WMI cookbook on Win10 #36

Closed McSpidey closed 5 years ago

McSpidey commented 7 years ago

I'm trying some of the examples from http://timgolden.me.uk/python/wmi/cookbook.html but they're not working with the PyMI library. Are they working for anyone else?

The monitoring example crashes with a memory error for example: " import wmi c = wmi.WMI() process_watcher = c.Win32_Process.watch_for("creation") while True: new_process = process_watcher() print new_process.Caption "

claudiubelu commented 7 years ago

Hi, we're using event listeners often through PyMI, and here is an example:

[1] https://github.com/openstack/os-win/blob/master/os_win/utils/network/networkutils.py#L267

It's basically waiting for a given event_type (creation / deletion - InstanceCreationEvent / InstanceDeletionEvent) of Msvm_SyntheticEthernetPortSettingData objects (Hyper-V VM NICs).

From what I can see, PyMI only accepts raw WQLs for watch_for. From my experience, using a WQL query offers a lot more flexibility over what is returned by the watcher, as you can see here [2]. But, for simpler scenarios, we could build the WQL query based on the given arguments.

[2] https://github.com/openstack/os-win/blob/master/os_win/utils/compute/clusterutils.py#L88

Your code would work like this:

import wmi
c = wmi.WMI()
wql = "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_Process'"
process_watcher = c.Win32_Process.watch_for(wql)
for i in range(20):
    try:
        # milliseconds
        new_process = process_watcher(2000)
        print new_process.Caption
    except wmi.x_wmi_timed_out:
        pass

Best regards,

Claudiu Belu

McSpidey commented 6 years ago

Thanks, that runs for me and functions. Oddly python still crashes but this time it's when the loop quits, so maybe something isn't unloading correctly?