[英]How to get exit code when using Python subprocess communicate method?
使用 Python 的subprocess
模块和communicate()
方法时如何检索退出代码?
相关代码:
import subprocess as sp
data = sp.Popen(openRTSP + opts.split(), stdout=sp.PIPE).communicate()[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 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
。
.poll()
将更新返回码。
尝试
child = sp.Popen(openRTSP + opts.split(), stdout=sp.PIPE)
returnCode = child.poll()
此外,在.poll()
之后,返回代码在对象中可用作为child.returncode
。
您应该首先确保进程已完成运行并且已使用.wait
方法读出返回码。 这将返回代码。 如果你想获得它以后,它的存储.returncode
在Popen
对象。
只是为了指出一个常见的误解,你应该尽可能避免Popen
。 要引用文档,
调用子流程的推荐方法是对其可以处理的所有用例使用
run()
function。 对于更高级的用例,可以直接使用底层的Popen
接口。
如果您只想运行一个子进程并等待它完成,那是一行带有subprocess.run
或其遗留兄弟subprocess.call
和subprocess.check_output
的代码,您不需要复制/粘贴和/或了解低级Popen
object 所需的communicate
和wait
等方法的复杂性。
import subprocess
proc = subprocess.run(
[openRTSP] + opts.split(),
capture_output=True,
# avoid having to explicitly encode
text=True)
data = proc.stdout
result = proc.returncode
如果您不想从进程中捕获 output ,可以将capture_output=True
替换为stdout=subprocess.DEVNULL
(对于stderr
可能类似); 在两者都没有的情况下,output 将简单地显示给用户,不受 Python 的控制。
此外,除非您在opts
中的选项完全微不足道,否则通常将此处的常规字符串split()
替换为shlex.split()
,后者了解如何处理带引号的字符串。
exitcode = data.wait()
。 如果子进程写入标准输出/错误,和/或从标准输入读取,并且没有对等点,则子进程将被阻止。
这对我有用。 它还打印子进程返回的输出
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
调用process.communicate()
后使用process.wait()
process.communicate()
。
例如:
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)
请看评论。
代码:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.