【问题标题】:How to get exit code when using Python subprocess communicate method?使用 Python 子进程通信方法时如何获取退出代码?
【发布时间】:2011-08-03 15:34:50
【问题描述】:

使用 Python 的 subprocess 模块和 communicate() 方法时如何检索退出代码?

相关代码:

import subprocess as sp
data = sp.Popen(openRTSP + opts.split(), stdout=sp.PIPE).communicate()[0]

我应该换一种方式吗?

【问题讨论】:

    标签: python subprocess


    【解决方案1】:

    请查看 cmets。

    代码:

    import subprocess
    
    
    class MyLibrary(object):
    
        def execute(self, cmd):
            return subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True,)
          
        def list(self):
            command = ["ping", "google.com"]
            sp = self.execute(command)
            status = sp.wait()  # will wait for sp to finish
            out, err = sp.communicate()
            print(out)
            return status # 0 is success else error
    
    
    test = MyLibrary()
    
    print(test.list())
    

    输出:

    C:\Users\shita\Documents\Tech\Python>python t5.py
    
    Pinging google.com [142.250.64.78] with 32 bytes of data:
    Reply from 142.250.64.78: bytes=32 time=108ms TTL=116
    Reply from 142.250.64.78: bytes=32 time=224ms TTL=116
    Reply from 142.250.64.78: bytes=32 time=84ms TTL=116
    Reply from 142.250.64.78: bytes=32 time=139ms TTL=116
    
    Ping statistics for 142.250.64.78:
        Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
        Minimum = 84ms, Maximum = 224ms, Average = 138ms
    
    0
    

    【讨论】:

      【解决方案2】:

      在调用process.communicate() 后使用process.wait()
      例如:

      import subprocess
      
      process = subprocess.Popen(['ipconfig', '/all'], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
      stdout, stderr = process.communicate()
      exit_code = process.wait()
      print(stdout, stderr, exit_code)
      

      【讨论】:

        【解决方案3】:

        这对我有用。它还打印子进程返回的输出

        child = subprocess.Popen(serial_script_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
            retValRunJobsSerialScript = 0
            for line in child.stdout.readlines():
                child.wait()
                print line           
            retValRunJobsSerialScript= child.returncode
        

        【讨论】:

          【解决方案4】:

          .poll() 将更新返回码。

          试试

          child = sp.Popen(openRTSP + opts.split(), stdout=sp.PIPE)
          returnCode = child.poll()
          

          此外,在调用.poll() 后,对象中的返回码为child.returncode

          【讨论】:

          • 当我这样做时 .poll() 是空的。我必须在 child.poll() 上面的行中运行 child.communicate() 才能使其正常工作。
          • 我认为您的意思是使用 .wait() 而不是 .poll(),根据文档:docs.python.org/3/library/subprocess.html。请注意, .wait() 需要一个可选的超时参数,这很方便。
          【解决方案5】:

          Popen.communicate 将在完成时设置returncode 属性(*)。以下是相关文档部分:

          Popen.returncode 
            The child return code, set by poll() and wait() (and indirectly by communicate()). 
            A None value indicates that the process hasn’t terminated yet.
          
            A negative value -N indicates that the child was terminated by signal N (Unix only).
          

          所以你可以这样做(我没有测试它,但它应该可以工作):

          import subprocess as sp
          child = sp.Popen(openRTSP + opts.split(), stdout=sp.PIPE)
          streamdata = child.communicate()[0]
          rc = child.returncode
          

          (*) 发生这种情况是因为它的实现方式:在设置线程读取子流后,它只调用wait

          【讨论】:

          • 这个例子对我有帮助,但如果例子不执行“import subprocess as sp”模式将标准导入为晦涩的缩写,那就太好了。虽然这会从后面的代码中删除 8 个字符,但它也使其难以理解和重用。
          • @uglycoyote 没有规定必须复制和粘贴。只需根据需要重新输入,就像 4 行一样。
          • @uglycoyote 您也可以将其编辑为from subprocess import Popen 之类的东西,然后只需使用Popen 而不是subprocess(or sp).Popen 我会说这可能会增加可读性并缩短行数
          • 是的...必须调用process.communicate(),然后将returncode 分配给某个变量。如果在调用communicate之前完成分配,则为None
          • 是否可以在不重定向管道的情况下显示返回码?我正在调用 bash 代码,我想在终端中实时查看输出
          【解决方案6】:

          您应该首先确保该进程已完成运行,并且已使用.wait 方法读取了返回码。这将返回代码。如果您想稍后访问它,它会以.returncode 的形式存储在Popen 对象中。

          【讨论】:

          • .communicate() 已经在等待子进程终止。
          【解决方案7】:

          exitcode = data.wait()。如果子进程写入标准输出/错误,和/或从标准输入读取,并且没有对等点,则子进程将被阻塞。

          【讨论】:

            猜你喜欢
            • 2021-03-17
            • 1970-01-01
            • 2018-10-23
            • 1970-01-01
            • 1970-01-01
            • 2011-04-21
            • 2014-01-29
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多