[python-win32] Maybe the wrong newsgroup to ask ?

Stef Mientki stef.mientki at gmail.com
Wed Jan 27 20:45:56 CET 2010


On 27-01-2010 20:07, Tim Roberts wrote:
> Stef Mientki wrote:
>    
>> I'm trying to kill processes,
>> I started myself with subprocess.popen,
>> under windows XP, Python 2.6
>>
>> Why are the subprocess.Popen methods kill() and terminate () not working,
>> while a simple suggestion from this newsgroup, shown below, works
>> perfect ?
>> ...
>>
>>      My_Process = subprocess.popen ( ...)
>>      handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0,
>> My_Process.PID)
>>      try:
>>        win32api.TerminateProcess(handle,0)
>>        win32api.CloseHandle(handle)
>>      except:   #the process might already be closed by the user
>>        pass
>>      
> There shouldn't be any difference.  If you look at the code in
> subprocess. it calls the same API.  The handle returned from
> CreateProcess has all access rights, including PROCESS_TERMINATE.
>
> Can you show us your original code?
>
>    
Good Question Tim.
Showing the code is always difficult for me, because I encapsulate these 
complex things.
The code I gave above doesn't work either :-)
I use a kill method that also kills the children and seems to be the 
essential thing is this situation,
(btw why are there children ?)
thanks for the tip,
cheers,
Stef

import os
import subprocess
import time

if __name__ == '__main__':
   My_Path = os.path.split ( __file__ ) [0]
   cwd = os.path.join ( My_Path, '..',
       'Pylab_Works', 'pylab_works_programs', 'PyJamas_Test')
   filename = os.path.join ( cwd, 'Een_Vraag_Test.py' )

   Process =  subprocess.Popen ( [ 'python', filename ],
                                   cwd   = cwd ,
                                   shell =  ( os.name == 'nt') )

   print Process.pid

   time.sleep ( 5 )
   Process.kill ()
   #Process.terminate ()
   print 'Kill or terminate Doesnt succeed'

   time.sleep ( 2 )
   import win32api
   import win32con
   handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, Process.pid)
   try:
     win32api.TerminateProcess(handle,0)
     win32api.CloseHandle(handle)        #close api
   except:   #the process might already be closed by the user
     pass
   print 'This still Doesnt works'


   time.sleep ( 2 )
   PID = Process.pid
   Children = []
   import win32com.client
   WMI = win32com.client.GetObject ( 'winmgmts:' )
   processes = WMI.InstancesOf ( 'Win32_Process' )
   for process in processes:
     pid    = process.Properties_ ( 'ProcessID' ).Value
     parent = process.Properties_ ( 'ParentProcessId' ).Value
     if parent == PID :
       Children.append ( pid )

   #Insert the parent at the top
   Children.insert ( 0, PID )

   import win32api
   import win32con
   for PID in Children :
     handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, PID)
     try:
       win32api.TerminateProcess(handle,0)
       win32api.CloseHandle(handle)
     except:   #the process might already be closed by the user
       pass
   print 'Finally this works'



More information about the python-win32 mailing list