subprocess.call python 中的退出代码

subprocess.call exit code in python

我有一个用例,我通过 python 子进程执行 java jar。我怎样才能知道子流程是成功完成还是由于某些异常而终止。子进程执行 return 退出代码,但 jar 不处理 returning 适当的退出代码。 还有其他方法吗?

class Test(object):
     #constructor
     .
     .
     def execute(self):
         exit_code = subprocess.call(['jar_path'])
         if not exit_code:
             return True
         else:
             return False

既然你说 -

Ques : does the jar report correct exceptions to stdout or stderr ?

Ans : yes it does support.

您可以使用 subprocess.Popen() along with .communicate()subprocess.PIPEstdout/stderr 获取数据并适当地解析它以确定是否有任何异常。

例子-

 def execute(self):
     proc = subprocess.Popen(['jar_path'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
     pout,perr = proc.communicate()
     if not perr:  #or however you want to check stderr/stdout
         return True
     else:
         return False