In Python 2.5, how do I kill a subprocess?

前端 未结 4 1070
灰色年华
灰色年华 2020-12-01 04:21

I am using the subprocess package in Python to run a subprocess, which I later need to kill. However, the documentation of the subprocess package states that the terminate()

4条回答
  •  Happy的楠姐
    2020-12-01 05:20

    To complete @Gareth's answer, on Windows you do:

    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)
    

    not quite as elegant as os.kill(theprocess.pid, 9), but it does work;-)

提交回复
热议问题