Python子进程Popen.communicate()相当于Popen.stdout.read()?

时间:2012-10-18 22:59:06

标签: python subprocess wait popen communicate

非常具体的问题(我希望):以下三个代码之间有什么区别?

(我希望它只是第一个不等待子进程完成,而第二个和第三个进行完成。但我需要确定这是唯一的区别...)

我也欢迎其他评论/建议(尽管我已经很清楚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

感谢。

1 个答案:

答案 0 :(得分:38)

如果您查看subprocess.communicate()的来源,它会显示差异的完美示例:

def communicate(self, input=None):
    ...
    # Optimization: If we are only using one pipe, or no pipe at
    # all, using select() or threads is unnecessary.
    if [self.stdin, self.stdout, self.stderr].count(None) >= 2:
        stdout = None
        stderr = None
        if self.stdin:
            if input:
                self.stdin.write(input)
            self.stdin.close()
        elif self.stdout:
            stdout = self.stdout.read()
            self.stdout.close()
        elif self.stderr:
            stderr = self.stderr.read()
            self.stderr.close()
        self.wait()
        return (stdout, stderr)

    return self._communicate(input)

您可以看到communicate确实使用了对stdoutstderr的阅读来电,并且还调用了wait()。这只是一个操作顺序问题。在您的情况下,因为您对stdout和stderr都使用PIPE,它会进入_communicate()

def _communicate(self, input):
    stdout = None # Return
    stderr = None # Return

    if self.stdout:
        stdout = []
        stdout_thread = threading.Thread(target=self._readerthread,
                                         args=(self.stdout, stdout))
        stdout_thread.setDaemon(True)
        stdout_thread.start()
    if self.stderr:
        stderr = []
        stderr_thread = threading.Thread(target=self._readerthread,
                                         args=(self.stderr, stderr))
        stderr_thread.setDaemon(True)
        stderr_thread.start()

    if self.stdin:
        if input is not None:
            self.stdin.write(input)
        self.stdin.close()

    if self.stdout:
        stdout_thread.join()
    if self.stderr:
        stderr_thread.join()

    # All data exchanged.  Translate lists into strings.
    if stdout is not None:
        stdout = stdout[0]
    if stderr is not None:
        stderr = stderr[0]

    # Translate newlines, if requested.  We cannot let the file
    # object do the translation: It is based on stdio, which is
    # impossible to combine with select (unless forcing no
    # buffering).
    if self.universal_newlines and hasattr(file, 'newlines'):
        if stdout:
            stdout = self._translate_newlines(stdout)
        if stderr:
            stderr = self._translate_newlines(stderr)

    self.wait()
    return (stdout, stderr)

这使用线程一次从多个流中读取。然后它最后调用wait()

总结一下:

  1. 此示例一次从一个流中读取,而不是等待它完成该过程。
  2. 此示例通过内部线程同时从两个流中读取,并等待它完成该过程。
  3. 此示例等待进程完成,然后一次读取一个流。正如你所提到的那样,如果写入流中的内容太多,就有可能陷入僵局。
  4. 此外,您在第2和第3个示例中不需要这两个import语句:

    from subprocess import communicate
    from subprocess import wait
    

    它们都是Popen对象的方法。