Python WMI Event Handler - Consumer

Dips dipankarweb at yahoo.co.in
Tue Aug 5 05:07:34 EDT 2003


Python Gurus,

Is it possible to write python WMI event consumer using event handler
rather than polling WMI?


I got the equivalent C# code snippet from
http://www.ondotnet.com/pub/a/dotnet/2003/04/07/wmi.html

using System;
using System.Management;
using System.Collections;

class WSMonitor
{
  bool pleaseContinue = true;

  public void doit()
  {
     try
     {
          
        int processId = 
            System.Diagnostics.Process.GetCurrentProcess().Id;
        int workingSet = 30000000; 
        string wqlQuery = String.Format(
           @"SELECT * FROM __InstanceModificationEvent WITHIN 1 
             WHERE TargetInstance ISA 'Win32_Process' AND 
                   TargetInstance.ProcessId = {0} AND 
                   TargetInstance.WorkingSetSize >= {1} AND 
                   PreviousInstance.WorkingSetSize < {2} ", 
           processId, workingSet, workingSet);
          
        WqlEventQuery query = new WqlEventQuery(wqlQuery);
        ManagementEventWatcher watcher = 
           new ManagementEventWatcher(query);
        watcher.EventArrived += 
           new EventArrivedEventHandler(onEvent);
  
        watcher.Start();
        ArrayList array = new ArrayList();
        for (int i = 0; pleaseContinue; ++i)
        {
           array.Add(1);
  
           if (i % 1000 == 0)
              System.Threading.Thread.Sleep(1);
        }
        
        watcher.Stop();
     }
     catch (ManagementException e)
     {
        Console.WriteLine("Management exception: " + e);
     }
  }
  
  public void onEvent(object sender, EventArrivedEventArgs e)
  {
        pleaseContinue = false;
        Console.WriteLine("You're a big boy now!");
  }
}

Thanks




More information about the Python-list mailing list