子进程是使用以下方式启动的
subprocess.Popen(arg)
有没有一种方法可以确保当父进程异常终止时它被杀死?我需要在Windows和Linux上都能实现这个功能。我知道这个Linux的解决方案。
编辑:
如果存在使用其他启动进程的方法的解决方案,则可以放宽使用subprocess.Popen(arg)
启动子进程的要求。
子进程是使用以下方式启动的
subprocess.Popen(arg)
有没有一种方法可以确保当父进程异常终止时它被杀死?我需要在Windows和Linux上都能实现这个功能。我知道这个Linux的解决方案。
编辑:
如果存在使用其他启动进程的方法的解决方案,则可以放宽使用subprocess.Popen(arg)
启动子进程的要求。
嘿,我昨天也在研究这个问题!假设您无法更改子程序:
在Linux上,prctl(PR_SET_PDEATHSIG, ...)
可能是唯一可靠的选择。(如果绝对必须杀死子进程,则可能需要将死亡信号设置为SIGKILL而不是SIGTERM;您链接的代码使用了SIGTERM,但子进程有忽略SIGTERM的选项。)
win32api
模块执行此操作的示例代码。该代码使用CreateProcess
启动子进程,而不是subprocess.Popen
。原因是他们需要获取生成的子进程的“进程句柄”,并且CreateProcess
默认返回此句柄。如果您更喜欢使用subprocess.Popen
,则以下是从该答案中复制的未经测试的代码,该代码使用subprocess.Popen
和OpenProcess
代替CreateProcess
:import subprocess
import win32api
import win32con
import win32job
hJob = win32job.CreateJobObject(None, "")
extended_info = win32job.QueryInformationJobObject(hJob, win32job.JobObjectExtendedLimitInformation)
extended_info['BasicLimitInformation']['LimitFlags'] = win32job.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
win32job.SetInformationJobObject(hJob, win32job.JobObjectExtendedLimitInformation, extended_info)
child = subprocess.Popen(...)
# Convert process id to process handle:
perms = win32con.PROCESS_TERMINATE | win32con.PROCESS_SET_QUOTA
hProcess = win32api.OpenProcess(perms, False, child.pid)
win32job.AssignProcessToJobObject(hJob, hProcess)
从技术上讲,在Popen
和OpenProcess
调用之间,如果子进程在此期间意外终止,会存在微小的竞争条件。您可以决定是否需要担心这个问题。
使用作业对象的一个缺点是,当在Vista或Win7上运行时,如果您的程序是从Windows shell(例如通过单击图标)启动的,则可能会已经分配了作业对象并且尝试创建新的作业对象将失败。Win8可以解决这个问题(通过允许嵌套作业对象),或者如果您的程序是从命令行运行,则应该没问题。
如果您可以修改子进程(例如在使用multiprocessing
时),那么最好的选择可能是以某种方式将父进程的PID传递给子进程(例如作为命令行参数或在multiprocessing.Process
的args=
参数中),然后执行以下操作:
os.getppid()
,如果返回值不再与父进程传递的 pid 匹配,则调用os._exit()
。(这种方法可移植到所有 Unix 系统,包括 OS X,而prctl
技巧是专门针对 Linux 的。)OpenProcess
和 os.waitpid
。以下是使用 ctypes 的示例:from ctypes import WinDLL, WinError
from ctypes.wintypes import DWORD, BOOL, HANDLE
# Magic value from http://msdn.microsoft.com/en-us/library/ms684880.aspx
SYNCHRONIZE = 0x00100000
kernel32 = WinDLL("kernel32.dll")
kernel32.OpenProcess.argtypes = (DWORD, BOOL, DWORD)
kernel32.OpenProcess.restype = HANDLE
parent_handle = kernel32.OpenProcess(SYNCHRONIZE, False, parent_pid)
# Block until parent exits
os.waitpid(parent_handle, 0)
os._exit(0)
kill -9
命令),那么它就没有机会杀死子进程了。 - Nathaniel J. Smithwin32job.TerminateJobObject(hJob, hProcess)
。 - DarkLightPopen对象提供了terminate和kill方法。
https://docs.python.org/2/library/subprocess.html#subprocess.Popen.terminate
这些方法可以为你发送SIGTERM和SIGKILL信号。 你可以执行类似以下的操作:
from subprocess import Popen
p = None
try:
p = Popen(arg)
# some code here
except Exception as ex:
print 'Parent program has exited with the below error:\n{0}'.format(ex)
if p:
p.terminate()
更新:
你是正确的-以上代码无法保护免受硬崩溃或某人杀死进程的影响。在这种情况下,您可以尝试将子进程封装在一个类中,并采用轮询模型来监视父进程。请注意,psutil是非标准的。
import os
import psutil
from multiprocessing import Process
from time import sleep
class MyProcessAbstraction(object):
def __init__(self, parent_pid, command):
"""
@type parent_pid: int
@type command: str
"""
self._child = None
self._cmd = command
self._parent = psutil.Process(pid=parent_pid)
def run_child(self):
"""
Start a child process by running self._cmd.
Wait until the parent process (self._parent) has died, then kill the
child.
"""
print '---- Running command: "%s" ----' % self._cmd
self._child = psutil.Popen(self._cmd)
try:
while self._parent.status == psutil.STATUS_RUNNING:
sleep(1)
except psutil.NoSuchProcess:
pass
finally:
print '---- Terminating child PID %s ----' % self._child.pid
self._child.terminate()
if __name__ == "__main__":
parent = os.getpid()
child = MyProcessAbstraction(parent, 'ping -t localhost')
child_proc = Process(target=child.run_child)
child_proc.daemon = True
child_proc.start()
print '---- Try killing PID: %s ----' % parent
while True:
sleep(1)
p.terminate()
?我正在寻找在Windows上启动一个进程的方法,使它能够在父进程无论因何原因而终止时退出。这在Linux上是可行的。 - user443854if __name__ == "__main__":
块。有了它,就更有意义了!尽管如此,与使用操作系统级工具(在Linux和Windows上都可用)相比,这种方法似乎过于复杂且不可靠,并且为问题创造了新的机会 - 例如,当前编写的代码使得父进程无法监控子进程的生命周期或获取退出码,并且如果运行多个子进程,将会泄漏看门狗进程。 - Nathaniel J. Smithimport psutil, os
def kill_proc_tree(pid, including_parent=True):
parent = psutil.Process(pid)
children = parent.children(recursive=True)
for child in children:
child.kill()
gone, still_alive = psutil.wait_procs(children, timeout=5)
if including_parent:
parent.kill()
parent.wait(5)
def func(x):
print("killed")
if anotherproc:
kill_proc_tree(anotherproc.pid)
kill_proc_tree(os.getpid())
import win32api,shlex
win32api.SetConsoleCtrlHandler(func, True)
PROCESSTORUN="your process"
anotherproc=None
cmdline=f"/c start /wait \"{PROCESSTORUN}\" "
anotherproc=subprocess.Popen(executable='C:\\Windows\\system32\\cmd.EXE', args=shlex.split(cmdline,posix="false"))
...
run program
...
据我所知,PR_SET_PDEATHSIG
解决方案在父进程中运行任何线程时可能导致死锁,因此我不想使用它,而是找到了另一种方法。我创建了一个单独的自动终止进程,它会检测其父进程何时完成,并杀死作为其目标的其他子进程。
要实现这一点,您需要pip install psutil
,然后编写类似以下代码的内容:
def start_auto_cleanup_subprocess(target_pid):
cleanup_script = f"""
import os
import psutil
import signal
from time import sleep
try:
# Block until stdin is closed which means the parent process
# has terminated.
input()
except Exception:
# Should be an EOFError, but if any other exception happens,
# assume we should respond in the same way.
pass
if not psutil.pid_exists({target_pid}):
# Target process has already exited, so nothing to do.
exit()
os.kill({target_pid}, signal.SIGTERM)
for count in range(10):
if not psutil.pid_exists({target_pid}):
# Target process no longer running.
exit()
sleep(1)
os.kill({target_pid}, signal.SIGKILL)
# Don't bother waiting to see if this works since if it doesn't,
# there is nothing else we can do.
"""
return Popen(
[
sys.executable, # Python executable
'-c', cleanup_script
],
stdin=subprocess.PIPE
)
这与我之前没有注意到的https://dev59.com/F2Ag5IYBdhLWcg3wlLuh#23436111类似,但我认为我想出的方法更容易使用,因为需要清理的进程是直接由父进程创建的。还要注意的是,在终止序列期间,不必轮询父进程的状态,尽管如果您想尝试像在此示例中一样终止、监视,然后在终止未能迅速工作时杀死目标子进程,则仍需要使用psutil
并轮询目标子进程的状态。
ps
输出中轻松识别自动终止进程,可以安装 setproctitle 包。然后,自动终止脚本可以使用该包来设置其自己的进程名称。 - Steve Jorgensen