|
Description:
The new subprocess module in Python 2.4 (also available for 2.2 and 2.3 at http://effbot.org/downloads/#subprocess) allows access to the handle of any newly created subprocess. You can use this handle to terminate subprocesses using either ctypes or the pywin32 extensions.
Source: Text Source
import subprocess
process = subprocess.Popen(['python.exe', '-c', 'while 1: pass'])
import win32api
win32api.TerminateProcess(int(process._handle), -1)
import ctypes
ctypes.windll.kernel32.TerminateProcess(int(process._handle), -1)
import win32api
PROCESS_TERMINATE = 1
handle = win32api.OpenProcess(PROCESS_TERMINATE, False, process.pid)
win32api.TerminateProcess(handle, -1)
win32api.CloseHandle(handle)
import ctypes
PROCESS_TERMINATE = 1
handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, process.pid)
ctypes.windll.kernel32.TerminateProcess(handle, -1)
ctypes.windll.kernel32.CloseHandle(handle)
Discussion:
You can use either pywin32 or ctypes to call the Windows TerminateProcess API. Just pick one - there is no need for both. If you don't like accessing the undocumented _handle member of the object returned by Popen, then you can achieve the same result using the documented pid member and the Windows OpenProcess API.
|
|
Add comment
|
|
Number of comments: 3
Only on Python > 2.2, stewart midwinter, 2005/04/28
Note that the ctypes solution is only available to you if you are using Python 2.3 or higher. You can find it here: http://starship.python.net/crew/theller/ctypes/
Add comment
You can add this method, Claveau Michel, 2005/11/15
# Kill the process with windows TASKKILL utility
import os
os.popen('TASKKILL /PID '+str(process.pid)+' /F')
Add comment
You can add this method, Claveau Michel, 2005/11/15
# Kill the process with windows TASKKILL utility
import os
os.popen('TASKKILL /PID '+str(process.pid)+' /F')
Add comment
|
|
|