Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 子进程调用避免在出错后停止_Python_Subprocess - Fatal编程技术网

Python 子进程调用避免在出错后停止

Python 子进程调用避免在出错后停止,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"])

我在for循环中调用一个程序a
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"