【问题标题】:python subprocess.Popen capture lots of outputpython subprocess.Popen 捕获大量输出
【发布时间】:2012-07-04 11:32:44
【问题描述】:

我正在从 python 脚本调用 subprocess.Popen,然后调用通信

process = subprocess.Popen(cmds, shell=shell, stdout=subprocess.PIPE,  
                               stderr=subprocess.PIPE)  

stdout, stderr = process.communicate()

这个问题是标准输出被缓冲并且只会捕获一定数量的字符。

问题是,我怎样才能获得我生成的进程的完整标准输出和完整标准错误?

【问题讨论】:

  • 你应该把所有的进程都拿出来&错误。可以扔样品吗?
  • 我参考了部分文档docs.python.org/library/subprocess.html 注意读取的数据是在内存中缓冲的,所以如果数据量很大或没有限制,请不要使用此方法。输出是太大而无法在线发布的编译我将示例放在eng.utah.edu/~rweber/python_out.txt
  • 只要不耗尽系统内存(或出现超出python字符串最大长度的错误)就会得到所有输出
  • 我会再试一次,看看是否抛出了错误,但我得到了很多输出。

标签: python subprocess


【解决方案1】:

几个点。

如果您将 PIPE 用于 stdout/stderr 并且 Popened 进程开始写入大量数据,并且在您的进程中您没有读取任何 stdout/stderr,则 Popened 进程将阻塞某种表示为

Note

Do not use stdout=PIPE or stderr=PIPE with this function. As the pipes are not being read in the current process, the child process may block if it generates enough output to a pipe to fill up the OS pipe buffer.

关于您指出的其他注释。仅当您使用 stdout=None,stderr=None 进行 Popen 时才适用。在这种情况下,所有 Popened process' out/err 都将存储在内存中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 2011-11-26
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 2020-02-29
    相关资源
    最近更新 更多