使用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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Popen.communicate
将在完成时设置returncode
属性(*)。这是相关的文档部分:所以你可以这样做(我没有测试它,但它应该可以工作):
(*)发生这种情况是因为它的实现方式:设置线程读取子流后,它只是调用 <代码>等待。
Popen.communicate
will set thereturncode
attribute when it's done(*). Here's the relevant documentation section:So you can just do (I didn't test it but it should work):
(*) This happens because of the way it's implemented: after setting up threads to read the child's streams, it just calls
wait
.只是为了指出一个常见的误解,您应该尽可能避免
Popen
。引用文档,如果您只想运行子流程并等待其完成,则只需使用
subprocess.run
或其旧同级subprocess.call
和编写一行代码即可subprocess.check_output
,并且您不需要复制/粘贴和/或了解低层所需的communicate
和wait
等方法的复杂性级别Popen
对象。如果您不想捕获进程的输出,可以将
capture_output=True
替换为stdout=subprocess.DEVNULL
(对于stderr
也许类似)代码>);如果两者都没有,输出将简单地显示给用户,不受 Python 的控制。另外,除非
opts
中的选项完全微不足道,否则通常将此处的常规字符串split()
替换为shlex.split()
,它了解如何处理带引号的字符串。Just to point out a common misconception, you should avoid
Popen
always when you can. To quote the documentation,If you just want to run a subprocess and wait for it to finish, that's a single line of code with
subprocess.run
or its legacy siblingssubprocess.call
andsubprocess.check_output
, and you don't need to copy/paste and/or understand the intricacies of thecommunicate
andwait
etc methods required around the low-levelPopen
object.If you don't want to capture the output from the process, maybe replace
capture_output=True
withstdout=subprocess.DEVNULL
(and perhaps similarly forstderr
); in the absence of either, the output will simply be displayed to the user, outside of Python's control.Also, unless your options in
opts
are completely trivial, generally replace the regular stringsplit()
here withshlex.split()
which understands how to cope with quoted strings..poll()
将更新返回码。另外,在调用
.poll()
后,对象中的返回码可作为child.returncode
使用。.poll()
will update the return code.Try
In addition, after
.poll()
is called the return code is available in the object aschild.returncode
.您应该首先确保进程已完成运行,并且已使用
.wait
方法读出返回码。这将返回代码。如果您想稍后访问它,它将作为.returncode
存储在Popen
对象中。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 thePopen
object.在调用
process.communicate()
后使用process.wait()
。例如:
Use
process.wait()
after you callprocess.communicate()
.For example:
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.这对我有用。它还打印子进程返回的输出
This worked for me. It also prints the output returned by the child process
请看评论。
代码:
输出:
Please see the comments.
Code:
Output: