如果任何一个子进程出错,则终止所有子进程
Terminate all subprocesses if any one of subprocess has error
我正在尝试同时 运行 两个子进程。我当前的程序看起来如果其中一个子进程出错并终止,则 1 分钟后它无法重新启动。它需要等待另一个子进程也失败并且它们一起启动。我找到了一些关于如何终止特定子进程的帖子,但是我怎么能(1)如果一个子进程出错,所有子进程将被终止并在 1 分钟后重新启动?或者 (2) 有错误的子进程可以在 1 分钟后重新启动而无需等待另一个 运行ning 子进程?如果我改变它会起作用
proc.wait()
至
proc.kill()
在下面的代码中?有没有更好的处理方法?
import sys
import subprocess
import os
import time
def executeSomething():
# This setting is very important to avoid errors
os.environ['PYTHONIOENCODING'] = 'utf-8'
procs = []
files = ["TwitterDownloader_1.py","TwitterDownloader_2.py"]
try:
for i in files:
proc = subprocess.Popen([sys.executable,i])
procs.append(proc)
for proc in procs:
proc.wait()
except AttributeError:
print 'Attribute Error found and skiped'
time.sleep(61)
while True:
executeSomething()
运行一个检查进程状态的循环怎么样?
像这样:
for proc in procs:
if proc.poll() is not None: # it has terminated
# check returncode and handle success / failure
我正在尝试同时 运行 两个子进程。我当前的程序看起来如果其中一个子进程出错并终止,则 1 分钟后它无法重新启动。它需要等待另一个子进程也失败并且它们一起启动。我找到了一些关于如何终止特定子进程的帖子,但是我怎么能(1)如果一个子进程出错,所有子进程将被终止并在 1 分钟后重新启动?或者 (2) 有错误的子进程可以在 1 分钟后重新启动而无需等待另一个 运行ning 子进程?如果我改变它会起作用
proc.wait()
至
proc.kill()
在下面的代码中?有没有更好的处理方法?
import sys
import subprocess
import os
import time
def executeSomething():
# This setting is very important to avoid errors
os.environ['PYTHONIOENCODING'] = 'utf-8'
procs = []
files = ["TwitterDownloader_1.py","TwitterDownloader_2.py"]
try:
for i in files:
proc = subprocess.Popen([sys.executable,i])
procs.append(proc)
for proc in procs:
proc.wait()
except AttributeError:
print 'Attribute Error found and skiped'
time.sleep(61)
while True:
executeSomething()
运行一个检查进程状态的循环怎么样?
像这样:
for proc in procs:
if proc.poll() is not None: # it has terminated
# check returncode and handle success / failure