Python 子进程调用避免在出错后停止
我在for循环中调用一个程序aPython 子进程调用避免在出错后停止,python,subprocess,Python,Subprocess,我在for循环中调用一个程序asubprocess.call(),然后它会做一些事情。但是,在被调用的程序崩溃后(不,我无法避免),python脚本终止 守则: with open("runtests.output", "w") as output: for i in range(1,5): for j in range(2,5): subprocess.call(["runprogram", "with", "arguments"])
subprocess.call()
,然后它会做一些事情。但是,在被调用的程序崩溃后(不,我无法避免),python脚本终止
守则:
with open("runtests.output", "w") as output:
for i in range(1,5):
for j in range(2,5):
subprocess.call(["runprogram", "with", "arguments"])
output.write(str(i) + str(j))
output.write(str(resource.getrusage(resource.RUSAGE_CHILDREN)))
output.write("\n")
在这种情况下,runprogram with arguments
在某个点失败,即在内部for循环的第三个循环,然后整个脚本停止。一、 但是,您希望脚本继续调用子进程,直到所有循环都循环完毕。
那么如何避免这种行为呢
编辑:
尝试
实际输出是两次被否定的中止
,这是在runprogram
的主进程中,但随后来自runprogram
的子进程的中止
,然后整个脚本失败。也许这与此有关
另外,
“Ohai”
未打印…定义失败。它失败是因为它不能执行运行程序,还是因为被调用的运行程序失败。如果抛出非零退出代码,子流程调用模块不应停止。所以我假设它与实际的子进程调用有问题。您的最佳选择是将其放入try/except循环。可能会将失败的系统置于try:except:pass
?
try:
call and writes here
except:
print "Ohai"