如何使用 python 中的父进程重新启动失败的进程
How to restart failed process with parent process in python
代码:
execs = ['C:\Users\XYZ\PycharmProjects\Task1\dist\multiof1.exe',
'C:\Users\XYZ\PycharmProjects\Task2\dist\multiof2.exe',
'C:\Users\XYZ\PycharmProjects\Task3\dist\multiof3.exe',
'C:\Users\XYZ\PycharmProjects\failedprocess\dist\multiof4.exe'
]
print('Parent Process id : ', os.getpid())
process = [subprocess.Popen(exe) for exe in execs]
for proc in process:
proc.wait()
print('Child Process id : ', proc.pid)
if proc.poll() is not None:
if proc.returncode == 0:
print(proc.pid, 'Exited')
elif proc.returncode > 0:
print('Failed:', proc.pid)
在上面,.exe 的子.exe 会失败,我需要从父进程重新启动那个失败的.exe。
我知道,上面的代码不是正确的实现,但我用谷歌搜索没有找到合适的解决方案。
任何支持都将帮助我了解有关子流程的更多信息。
我的评论是这样的:
import subprocess
import time
execs = [
"C:\Users\XYZ\PycharmProjects\Task1\dist\multiof1.exe",
"C:\Users\XYZ\PycharmProjects\Task2\dist\multiof2.exe",
"C:\Users\XYZ\PycharmProjects\Task3\dist\multiof3.exe",
"C:\Users\XYZ\PycharmProjects\failedprocess\dist\multiof4.exe",
]
class WrappedProcess:
def __init__(self, exe):
self.exe = exe
self.process = None
self.success = False
def check(self):
if self.success: # Nothing to do, we're already successful.
return
if self.process is None: # No current process, start one.
print("Starting", self.exe)
self.process = subprocess.Popen(self.exe)
return # Only poll on next check
if self.process.poll() is None: # Not quit yet.
return
if self.process.returncode == 0:
print("Finished successfully:", self.exe)
self.success = True
else:
print("Failed:", self.exe)
# Abandon the process; next check() will retry.
self.process = None
wrapped_processes = [WrappedProcess(exe) for exe in execs]
while True:
for proc in wrapped_processes:
proc.check()
if all(proc.success for proc in wrapped_processes):
print("All processes ended successfully")
break
time.sleep(1)
在这里添加“最大时间”功能也很容易(启动新进程时,存储当前时间;check()
如果进程过期则终止进程)。
代码:
execs = ['C:\Users\XYZ\PycharmProjects\Task1\dist\multiof1.exe',
'C:\Users\XYZ\PycharmProjects\Task2\dist\multiof2.exe',
'C:\Users\XYZ\PycharmProjects\Task3\dist\multiof3.exe',
'C:\Users\XYZ\PycharmProjects\failedprocess\dist\multiof4.exe'
]
print('Parent Process id : ', os.getpid())
process = [subprocess.Popen(exe) for exe in execs]
for proc in process:
proc.wait()
print('Child Process id : ', proc.pid)
if proc.poll() is not None:
if proc.returncode == 0:
print(proc.pid, 'Exited')
elif proc.returncode > 0:
print('Failed:', proc.pid)
在上面,.exe 的子.exe 会失败,我需要从父进程重新启动那个失败的.exe。
我知道,上面的代码不是正确的实现,但我用谷歌搜索没有找到合适的解决方案。
任何支持都将帮助我了解有关子流程的更多信息。
我的评论是这样的:
import subprocess
import time
execs = [
"C:\Users\XYZ\PycharmProjects\Task1\dist\multiof1.exe",
"C:\Users\XYZ\PycharmProjects\Task2\dist\multiof2.exe",
"C:\Users\XYZ\PycharmProjects\Task3\dist\multiof3.exe",
"C:\Users\XYZ\PycharmProjects\failedprocess\dist\multiof4.exe",
]
class WrappedProcess:
def __init__(self, exe):
self.exe = exe
self.process = None
self.success = False
def check(self):
if self.success: # Nothing to do, we're already successful.
return
if self.process is None: # No current process, start one.
print("Starting", self.exe)
self.process = subprocess.Popen(self.exe)
return # Only poll on next check
if self.process.poll() is None: # Not quit yet.
return
if self.process.returncode == 0:
print("Finished successfully:", self.exe)
self.success = True
else:
print("Failed:", self.exe)
# Abandon the process; next check() will retry.
self.process = None
wrapped_processes = [WrappedProcess(exe) for exe in execs]
while True:
for proc in wrapped_processes:
proc.check()
if all(proc.success for proc in wrapped_processes):
print("All processes ended successfully")
break
time.sleep(1)
在这里添加“最大时间”功能也很容易(启动新进程时,存储当前时间;check()
如果进程过期则终止进程)。