子流程模块中的 check_call check_output 调用和 Popen 方法之间的实际区别是什么? [英] What is a practical difference between check_call check_output call, and Popen methods in the subprocess module?
问题描述
老实说,我只是不明白非零"状态的行话来真正解释帮助页面上正在发生的事情或这意味着什么(甚至没有定义).有哪些使用python调用其他脚本的例子,其中
Honestly, I just don't understand the lingo of "non-zero" status to really interpret what's going on or what that means (it wasn't even defined) on help pages. What are some examples of using python to call other scripts in which these processes of
subprocess.callsubprocess.check_outputsubprocess.popen
真的不一样吗?你什么时候会使用其中的任何一个,这些方法的明确细节是什么?如果我想要简单的操作系统调用,我应该使用 os.system 吗?
really differ from each other? When would you use either of those, and what are the disambiguated details of these methods? Should I be using os.system instead if I wanted simple OS calls?
推荐答案
主要区别在于,popen
是一个非阻塞函数(意味着你可以继续执行程序而无需等待调用完成),call
和 check_output
是阻塞.
The main difference is that, while popen
is a non-blocking function (meaning you can continue the execution of the program without waiting the call to finish), both call
and check_output
are blocking.
另一个区别在于它们返回的内容:
The other difference is in what they return:
popen
返回一个Popen 对象
.call
返回returncode
属性.check_output
返回命令执行的输出.
方法 call
和 check_output
实际上是, 阻塞 popen
,使用 Popen 对象
.例如,您可以获得 returncode
属性通过调用 Popen.returncode()
.
The methods call
and check_output
are, in fact, blocking wrappers of popen
, using a Popen object
.
For example, you can get the returncode
attribute by calling Popen.returncode()
.
这篇关于子流程模块中的 check_call check_output 调用和 Popen 方法之间的实际区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!