subprocess.run:将 check=True 与 stdout=PIPE 结合使用是否安全?
subprocess.run: Is it safe to combine check=True with stdout=PIPE?
Python 3.5 引入了 run()
function in the subprocess
module 作为子流程调用的新推荐高级方法。
其中三位年龄较大(可用since Python 2.5 / 2.7) high-level API functions is check_call()
. The Python 3.5 documentation claims即check_call()
[...] is equivalent to:
run(..., check=True)
文档还警告不要将 subprocess.PIPE
作为 stdout
或 stderr
传递给 check_call()
:
Note
Do not use stdout=PIPE
or stderr=PIPE
with this function. The child process will block if it generates enough output to a pipe to fill up the OS pipe buffer as the pipes are not being read from.
由于是 "equivalent",此警告是否也适用于 run(..., check=True)
,即应该
subprocess.run(..., stdout=subprocess.PIPE, check=True)
和
subprocess.run(..., stderr=subprocess.PIPE, check=True)
也要避开? (documentation of run()
没有提到这个警告。)
Is it safe to combine check=True with stdout=PIPE?
是。
不应该使用 stdout=PIPE
作为 subprocess.check_call
(或 subprocess.call
)的参数的原因是这些实用函数不处理进程可能 recieve/produce。如果你想处理输出,你需要(在 subprocess.run
实现之前)使用 subprocess.check_output
,它专门处理输出,在 its own documentation 中,它表示等价于 run(..., check=True, stdout=PIPE).stdout
.这清楚地表明 subprocess.run(...,check=True,stdout=PIPE)
是有效的。
Python 3.5 引入了 run()
function in the subprocess
module 作为子流程调用的新推荐高级方法。
其中三位年龄较大(可用since Python 2.5 / 2.7) high-level API functions is check_call()
. The Python 3.5 documentation claims即check_call()
[...] is equivalent to:
run(..., check=True)
文档还警告不要将 subprocess.PIPE
作为 stdout
或 stderr
传递给 check_call()
:
Note
Do not use
stdout=PIPE
orstderr=PIPE
with this function. The child process will block if it generates enough output to a pipe to fill up the OS pipe buffer as the pipes are not being read from.
由于是 "equivalent",此警告是否也适用于 run(..., check=True)
,即应该
subprocess.run(..., stdout=subprocess.PIPE, check=True)
和
subprocess.run(..., stderr=subprocess.PIPE, check=True)
也要避开? (documentation of run()
没有提到这个警告。)
Is it safe to combine check=True with stdout=PIPE?
是。
不应该使用 stdout=PIPE
作为 subprocess.check_call
(或 subprocess.call
)的参数的原因是这些实用函数不处理进程可能 recieve/produce。如果你想处理输出,你需要(在 subprocess.run
实现之前)使用 subprocess.check_output
,它专门处理输出,在 its own documentation 中,它表示等价于 run(..., check=True, stdout=PIPE).stdout
.这清楚地表明 subprocess.run(...,check=True,stdout=PIPE)
是有效的。