Uri*_*Uri 28 python subprocess
我在Python中使用子进程包来运行子进程,我后来需要杀死它.但是,子进程包的文档声明terminate()函数仅在2.6中可用
我们使用2.5运行Linux,出于向后兼容的原因,我无法升级到2.6,有什么替代方案?我猜这些函数是方便的东西.
Gar*_*son 41
你在进程pid上调用os.kill.
os.kill(process.pid, signal.SIGKILL)
Run Code Online (Sandbox Code Playgroud)
你很好,因为你在Linux上.Windows用户运气不好.
Ale*_*lli 40
要完成@ Gareth的答案,请在Windows上执行以下操作:
import ctypes
PROCESS_TERMINATE = 1
handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, theprocess.pid)
ctypes.windll.kernel32.TerminateProcess(handle, -1)
ctypes.windll.kernel32.CloseHandle(handle)
Run Code Online (Sandbox Code Playgroud)
不是那么优雅os.kill(theprocess.pid, 9),但确实有效;-)