非常具体的问题(我希望):以下三个代码有什么区别?Python子进程Popen.communicate()等价于Popen.stdout.read()?
(我希望它是只有第一没有等待子进程来完成,而第二个和第三个做,但我需要确保这是唯一差异 ... )
我也欢迎其他评论/建议(虽然我已深知shell=True
危险和跨平台的限制)
注意的,我已经阅读Python subprocess interaction, why does my process work with Popen.communicate, but not Popen.stdout.read()?,而我不希望/需要互动与程序之后。
另外请注意,我已经读过Alternatives to Python Popen.communicate() memory limitations?但我并没有真正得到它...
最后请注意,我知道冥冥之中有当一个缓冲区填充了一个输出僵局的风险使用一种方法,但同时寻找在互联网上解释清楚我迷路了......
首先代码:
from subprocess import Popen, PIPE
def exe_f(command='ls -l', shell=True):
"""Function to execute a command and return stuff"""
process = Popen(command, shell=shell, stdout=PIPE, stderr=PIPE)
stdout = process.stdout.read()
stderr = process.stderr.read()
return process, stderr, stdout
二码:
from subprocess import Popen, PIPE
from subprocess import communicate
def exe_f(command='ls -l', shell=True):
"""Function to execute a command and return stuff"""
process = Popen(command, shell=shell, stdout=PIPE, stderr=PIPE)
(stdout, stderr) = process.communicate()
return process, stderr, stdout
第三码:
from subprocess import Popen, PIPE
from subprocess import wait
def exe_f(command='ls -l', shell=True):
"""Function to execute a command and return stuff"""
process = Popen(command, shell=shell, stdout=PIPE, stderr=PIPE)
code = process.wait()
stdout = process.stdout.read()
stderr = process.stderr.read()
return process, stderr, stdout
感谢。