问题描述
例如,我们在Windows上有2个可执行文件。 (EXE_1.exe,EXE_2.exe)
EXE_1.exe用参数调用EXE_2.exe。我想检测EXE_2何时关闭(已终止,被杀死等)
我无法在C ++中使用WaitForSingleObject或在python中使用Psutil进程等待。因为它是 First 可执行文件关闭时返回的。 (记住:第一个exe启动第二个exe并关闭自己)
所以我需要一个函数,该函数可以返回一个称为exe的pid或类似名称的exe。
我尝试了c ++中的GetChildProcessID,它可以工作,但是返回CMD.exe的pid。很好,如果我检查此cmd exe;当它关闭时,这意味着我创建的第二个exe文件也关闭了。但我希望获得第二个pid。
----解决:
info = subprocess.STARTUPINFO()
info.dwFlags = subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = Emrewin32con.SW_MINIMIZE
EXE = subprocess.Popen([EXE_KONUM],cwd=KLASOR_KONUM_EXE,creationflags=subprocess.CREATE_NO_WINDOW,startupinfo=info,stderr=subprocess.DEVNULL,stdout=subprocess.DEVNULL,shell=False)
def GetChildProcessIDList(pid):
prc = psutil.Process(pid)
return prc.children(recursive=True)
Childrens = GetChildProcessIDList(EXE.pid)
isCmd = False
for chcmd in Childrens:
if chcmd.name() == "cmd.exe":
isCmd=True
Childrens.remove(chcmd)
break
# list of Childrens is PID's.
解决方法
info = subprocess.STARTUPINFO()
info.dwFlags = subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = EmreWin32Con.SW_MINIMIZE
EXE = subprocess.Popen([EXE_KONUM],cwd=KLASOR_KONUM_EXE,creationflags=subprocess.CREATE_NO_WINDOW,startupinfo=info,stderr=subprocess.DEVNULL,stdout=subprocess.DEVNULL,shell=False)
def GetChildProcessIDList(pid):
prc = psutil.Process(pid)
return prc.children(recursive=True)
Childrens = GetChildProcessIDList(EXE.pid)
isCmd = False
for chcmd in Childrens:
if chcmd.name() == "cmd.exe":
isCmd=True
Childrens.remove(chcmd)
break
# variable Childrens: list of process created process PID's.