[python-win32] stopping process raises Win32 exception occurred releasing IUnknown at

Steffen Frömer steffen.froemer at gns-systems.de
Tue Jan 24 11:42:17 CET 2012


Hello,

i am trying to write a class, which starts a application and this 
application run other applications.
My class has a start and stop-method. The stop method also stop the 
child-processes of my started application.

At botton, i added a testclass, which represents my class. I am starting 
cmd.exe and this starts a notepad.exe. All is running inside a Thread. 
On calling my stop method, i search all childprocesses of my cmd.exe 
with help of WMI and terminate these PIDs. The terminate method of 
subprocess only terminate the "main"-process. All child-processes will 
remain.

All hints to resolve my issue are welcome.

best Regards,
Steffen


#----------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------

from subprocess import PIPE, Popen
import thread
import time
import logging
import os
import win32api
import pythoncom
import win32con
from wmi import WMI

class MyClass(object):

     def __init__(self):
         self.name = __name__
         self.process = None
         self.cmd = None
         self.PID = None
         self.returncode = None
         self.runtime = -1

         self.WMI_RUN = False
         self.WMI = None
     
#---------------------------------------------------------------------------
     def __startCommand(self):
         logging.debug("starting COMMAND")
         self.startTime = time.time()

         # start-cmd for starting TiscCenter
         self.cmd = '"{executable}" -cmd /k 
{argument}'.format(executable=os.environ['ComSpec'],argument=r'c:/windows/system32/notepad.exe')


         logging.debug("starting command: {0}".format(self.cmd))
         self.process = Popen(self.cmd, stdout=PIPE, stderr=PIPE)
         self.PID = self.process.pid
         self.std_out, self.std_err = self.process.communicate()

         self.returncode = self.process.returncode
         endTime = time.time()
         self.runtime = endTime - self.startTime

     
#---------------------------------------------------------------------------

     
#---------------------------------------------------------------------------
     def start(self):

         try:
             thread.start_new_thread(self.__startCommand, ())
         except BaseException, err:
             print(err)
     
#---------------------------------------------------------------------------

     
#---------------------------------------------------------------------------
     def stop(self):
         if self.process:
             self.process.terminate()
         else:
             logging.error("nothing to stop")
     
#---------------------------------------------------------------------------


     
#---------------------------------------------------------------------------
     def kill(self):

         print("Interface-count:",pythoncom._GetInterfaceCount())
         pythoncom.CoInitialize()
         try:
             ProcessList = []
             childList = []
             for process in WMI().Win32_Process():
                 if (process.ParentProcessId == self.PID) and 
(process.ProcessId != self.PID):
                     ProcessList.append(process.ProcessId)

             if ProcessList:
                 for processId in ProcessList:
                     childList.append(processId)

             childList.append(self.PID)
             print("PID-List:",childList)


             for pid in childList:
                 try:
                     handle = 
win32api.OpenProcess(win32con.PROCESS_TERMINATE, False, pid)
                 except BaseException, err:
                     logging.exception(err)
                 else:
                     try:
                         win32api.TerminateProcess(handle, 0)
                         win32api.CloseHandle(handle)
                     except BaseException, err:
                         logging.exception(err)
         except BaseException, err:
             logging.exception(err)

         finally:
             pythoncom.CoUninitialize()
             print("Interface-count:",pythoncom._GetInterfaceCount())
     
#---------------------------------------------------------------------------

if __name__ == '__main__':
     logging.basicConfig(
         level = logging.DEBUG,
         format = "[%(levelname)s] %(asctime)s : %(message)s",
     )

     a = MyClass()
     a.start()

     time.sleep(2.0)

     a.kill()


#----------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------
example output:
#----------------------------------------------------------------------------------------
[DEBUG] 2012-01-24 11:38:16,525 : starting COMMAND
[DEBUG] 2012-01-24 11:38:16,529 : starting command: 
"C:\Windows\system32\cmd.exe" -cmd /k c:/windows/system32/notepad.exe
('Interface-count:', 4)
('PID-List:', [12892, 13876])
('Interface-count:', 19)
Win32 exception occurred releasing IUnknown at 0x048fd870
Win32 exception occurred releasing IUnknown at 0x048fd808
Win32 exception occurred releasing IUnknown at 0x048f9238
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20120124/1e7b026b/attachment.html>


More information about the python-win32 mailing list