问题:使用Python子进程通讯方法时如何获取退出代码?

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

相关代码:

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

我应该以其他方式这样做吗?

How do I retrieve the exit code when using Python’s subprocess module and the communicate() method?

Relevant code:

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

Should I be doing this another way?


回答 0

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 hasnt 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

Popen.communicate will set the returncode attribute when it’s done(*). Here’s the relevant documentation section:

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).

So you can just do (I didn’t test it but it should work):

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

(*) This happens because of the way it’s implemented: after setting up threads to read the child’s streams, it just calls wait.


回答 1

您首先应确保该进程已完成运行,并且已使用该.wait方法读取了返回代码。这将返回代码。如果您以后想要访问它,则将其存储.returncodePopen对象中。

You should first make sure that the process has completed running and the return code has been read out using the .wait method. This will return the code. If you want access to it later, it’s stored as .returncode in the Popen object.


回答 2

.poll() 将更新返回码。

尝试

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

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

.poll() will update the return code.

Try

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

In addition, after .poll() is called the return code is available in the object as child.returncode.


回答 3

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

exitcode = data.wait(). The child process will be blocked If it writes to standard output/error, and/or reads from standard input, and there are no peers.


回答 4

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

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

This worked for me. It also prints the output returned by the child process

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。