Seite 1 von 1

WMI und threading

Verfasst: Montag 25. Mai 2009, 10:38
von haggi
Irgendwie scheinene sich Threading und wmi nicht zu verstehen.
Ich wollte einen thread im Hintergrund starten, der mir Prozesse überwacht. Sobald ich jedoch wmi mit drin habe bekomme ich Fehlermedungen.

In dem code unten kann ich getProc() ohne Probleme aufrufen, jedoch nicht aus dem thread heraus.

Hat jemand eine Ahnung ob sich das Problem lösen lässt?

Code: Alles auswählen

from threading import Thread
import time
import wmi

def getProc():
    c = wmi.WMI()
    for process in c.Win32_Process():
        print process

class ProcThread(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.runForever = True
            
    def run(self):
        while self.runForever:
            print "Executing nix..."
            getProc()
            time.sleep(10)
                    
    def stop(self):
        print "Stopping thread"
        self.runForever = False        
            

getProc()    
pt = ProcThread()
pt.start()

time.sleep(60)
Das ist der Fehler:

Code: Alles auswählen

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Ambient\Python\lib\threading.py", line 462, in __bootstrap
    self.run()
  File "C:\svn\render\renderfarm\src\threadTest.py", line 20, in run
    getProc()
  File "C:\svn\render\renderfarm\src\threadTest.py", line 7, in getProc
    c = wmi.WMI()
  File "C:\Ambient\Python\lib\wmi.py", line 1190, in connect
    handle_com_error (error_info)
  File "C:\Ambient\Python\lib\wmi.py", line 189, in handle_com_error
    raise x_wmi, "\n".join (exception_string)
x_wmi: -0x7ffbfe1c - Ungültige Syntax

Verfasst: Montag 25. Mai 2009, 17:11
von gerold
Hallo haggi!

http://www.python-forum.de/topic-13499.html

pythoncom.CoInitialize()
pythoncom.CoUninitialize()

Ansonsten, suche hier im Forum nach "threading com".

mfg
Gerold
:-)

Verfasst: Dienstag 26. Mai 2009, 09:07
von haggi
Supergenial! Funktionniert :)

Danke.