当使用Python的子进程模块和communication()方法时,我如何检索退出代码?
相关代码:
import subprocess as sp
data = sp.Popen(openRTSP + opts.split(), stdout=sp.PIPE).communicate()[0]
我应该换一种方法吗?
当使用Python的子进程模块和communication()方法时,我如何检索退出代码?
相关代码:
import subprocess as sp
data = sp.Popen(openRTSP + opts.split(), stdout=sp.PIPE).communicate()[0]
我应该换一种方法吗?
当前回答
当它完成时,popen . communication将设置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。
其他回答
只是指出一个常见的误解,你应该尽量避免Popen。引用文档,
推荐的方法 调用子流程是 使用run()函数 对于所有用例 它可以处理。 更高级的 用例,底层 Popen接口 可直接使用。
如果你只是想运行一个子进程并等待它完成,那就是使用subprocess.run或它的遗留兄弟子进程的一行代码。调用和子流程。你不需要复制/粘贴和/或理解围绕底层Popen对象所需的通信和等待等方法的复杂性。
import subprocess
proc = subprocess.run(
[openRTSP] + opts.split(),
capture_output=True,
# avoid having to explicitly encode
text=True)
data = proc.stdout
result = proc.returncode
如果不想从流程中捕获输出,可以将capture_output=True替换为stdout=subprocess。DEVNULL(可能与stderr类似);如果两者都不存在,则输出将简单地显示给用户,超出Python的控制范围。
此外,除非你在opts中的选项完全是微不足道的,否则通常用shlex.split()替换常规字符串split(),它理解如何处理带引号的字符串。
这对我很管用。它还打印子进程返回的输出
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
请看评论。
代码:
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
Exitcode = data.wait()。如果子进程写入标准输出/错误,和/或从标准输入读取,并且没有对等点,则子进程将被阻塞。
在调用process. communication()之后使用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)