相关疑难解决方法(0)

Python Popen.communicate()内存限制的替代方案?

我有以下大量的Python代码(运行v2.7)导致在MemoryError处理大(几GB)文件时抛出异常:

myProcess = Popen(myCmd, shell=True, stdout=PIPE, stderr=PIPE)
myStdout, myStderr = myProcess.communicate()
sys.stdout.write(myStdout)
if myStderr:
    sys.stderr.write(myStderr)
Run Code Online (Sandbox Code Playgroud)

在阅读文档时Popen.communicate(),似乎有一些缓冲:

注意读取的数据缓冲在内存中,因此如果数据大小很大或不受限制,请不要使用此方法.

有没有办法禁用此缓冲,或强制缓存在进程运行时定期清除?

我应该在Python中使用什么替代方法来运行将千兆字节数据流式传输到的命令stdout

我应该注意,我需要处理输出和错误流.

python memory stream popen

19
推荐指数
2
解决办法
5238
查看次数

运行命令并像在终端中一样近乎实时地获取它的stdout,stderr

我试图在Python中找到一种方法来运行其他程序:

  1. 正在运行的程序的stdout和stderr可以单独记录.
  2. 正在运行的程序的stdout和stderr可以近乎实时地查看,这样如果子进程挂起,用户就可以看到.(即我们不会在将stdout/stderr打印到用户之前等待执行完成)
  3. 奖励标准:正在运行的程序不知道它是通过python运行的,因此不会做意想不到的事情(比如chunk它的输出而不是实时打印,或退出因为它需要终端查看其输出) .这个小标准几乎意味着我们需要使用我认为的pty.

这是我到目前为止所得到的...方法1:

def method1(command):
    ## subprocess.communicate() will give us the stdout and stderr sepurately, 
    ## but we will have to wait until the end of command execution to print anything.
    ## This means if the child process hangs, we will never know....
    proc=subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, executable='/bin/bash')
    stdout, stderr = proc.communicate() # record both, but no way to print stdout/stderr in real-time
    print ' ######### REAL-TIME ######### '
    ########         Not Possible
    print ' ########## RESULTS ########## …
Run Code Online (Sandbox Code Playgroud)

python subprocess tty pexpect pty

15
推荐指数
1
解决办法
3702
查看次数

将子进程输出显示到stdout并重定向它

我正在通过Python的子进程模块运行脚本.目前我使用:

p = subprocess.Popen('/path/to/script', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result = p.communicate()
Run Code Online (Sandbox Code Playgroud)

然后我将结果打印到stdout.这很好,但由于脚本需要很长时间才能完成,我还希望从脚本到stdout的实时输出.我输出输出的原因是因为我想解析它.

python subprocess stdout

12
推荐指数
1
解决办法
9912
查看次数

换行子进程'stdout/stderr

我想捕获并显示我通过Python的子进程调用的进程的输出.

我以为我可以将我的文件类对象作为命名参数stdout和stderr传递

我可以看到它访问fileno属性 - 所以它正在对对象做一些事情.但是,write()永远不会调用该方法.我的方法是完全关闭还是我错过了什么?

class Process(object):
    class StreamWrapper(object):
        def __init__(self, stream):
            self._stream = stream
            self._buffer = []
        def _print(self, msg):
            print repr(self), msg
        def __getattr__(self, name):
            if not name in ['fileno']:
                self._print("# Redirecting: %s" % name)
            return getattr(self._stream, name)
        def write(self, data):
            print "###########"
            self._buffer.append(data)
            self._stream.write(data)
            self._stream.flush()
        def getBuffer(self):
            return self._buffer[:]
    def __init__(self, *args, **kwargs):
        print ">> Running `%s`" % " ".join(args[0])
        self._stdout = self.StreamWrapper(sys.stdout)
        self._stderr = self.StreamWrapper(sys.stderr)
        kwargs.setdefault('stdout', self._stdout)
        kwargs.setdefault('stderr', self._stderr)
        self._process = subprocess.Popen(*args, …
Run Code Online (Sandbox Code Playgroud)

python iostream subprocess stdout stderr

7
推荐指数
1
解决办法
5541
查看次数

记录 python 子进程的语法错误和未捕获的异常并将其打印到终端

问题

我一直在尝试编写一个程序来记录子进程的未捕获异常和语法错误。容易,对吧?只需将管道输送stderr到正确的位置即可。

然而,子进程是另一个 python 程序——我称之为test.py——需要运行,就好像它的输出/错误没有被捕获一样。也就是说,运行记录器程序需要看起来就像用户刚刚python test.py正常运行一样。

使问题进一步复杂化的是如果不使用则实际发送到的问题不幸的是,我不能,因为我无法控制使用错误记录器运行的文件。raw_inputstderrreadlineimport readline

笔记:

  • 我对运行这段代码的机器有相当的限制。我无法安装pexpect或编辑这些*customize.py文件(因为该程序将由许多不同的用户运行)。我真的觉得无论如何应该有一个 stdlib 解决方案......
  • 这仅适用于 Macs。
  • 这样做的动机是我是研究新程序员所犯错误的团队的一员。

我尝试过的

我尝试过以下方法,但没有成功:

  • 只是使用tee问题中的如何在使用带有管道的“tee”时将 stderr 写入文件?(未能产生raw_input提示);我在几个 SO 问题中发现的python 实现也tee有类似的问题
  • 覆盖sys.excepthook(无法使其适用于子进程)
  • 这个问题的最佳答案似乎很有希望,但它未能raw_input正确显示提示。
  • 日志记录模块似乎对于实际写入日志文件很有用,但似乎没有解决问题的关键
  • 自定义 stderr 阅读器
  • 无休止的谷歌搜索

python subprocess stderr

5
推荐指数
1
解决办法
2452
查看次数

如何使用自定义的类文件对象作为子进程stdout / stderr?

考虑下面的代码,其中subprocess.Popen产生了a。我想写入子流程stdoutstderr转到我的自定义文件对象的.write()方法,但是事实并非如此。

import subprocess

class Printer:

    def __init__(self):
        pass

    def write(self, chunk):
        print('Writing:', chunk)

    def fileno(self):
        return 0

    def close(self):
        return

proc = subprocess.Popen(['bash', '-c', 'echo Testing'], 
                        stdout=Printer(),
                        stderr=subprocess.STDOUT)
proc.wait()
Run Code Online (Sandbox Code Playgroud)

为什么.write()不使用该方法stdout=?在这种情况下,指定参数有什么用?

python subprocess

5
推荐指数
1
解决办法
2212
查看次数

杀死子进程后外壳挂起

我知道在 SO 上有很多类似的问题,比如这个这个,也许还有更多,但似乎没有一个适用于我的特定情况。我对subprocess.Popen()工作原理缺乏了解也无济于事。

我想要实现的是:启动一个子进程(一个命令行收音机播放器),它也向终端输出数据,也可以接收输入——等待一段时间——终止子进程——退出shell。我在 OSX 10.9 上运行 python 2.7

案例 1. 这将启动广播播放器(但仅限音频!),终止进程,退出。

import subprocess
import time

p = subprocess.Popen(['/bin/bash', '-c', 'mplayer http://173.239.76.147:8090'],
                     stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=False,
                     stderr=subprocess.STDOUT)
time.sleep(5)
p.kill()
Run Code Online (Sandbox Code Playgroud)

案例 2. 这将启动收音机播放器,输出诸如收音机名称、歌曲、比特率等信息并接受输入。它终止子进程,但它从不存在 shell,即使使用“Ctrl-C”,终端也无法使用。

p = subprocess.Popen(['/bin/bash', '-c', 'mplayer http://173.239.76.147:8090'],
                     shell=False)
time.sleep(5)
p.kill()
Run Code Online (Sandbox Code Playgroud)

关于如何做到这一点的任何想法?我什至在想,如果没有其他选择,是否有可能为子进程打开一个slave-shell(当然这也是我不知道的事情)。谢谢!

python subprocess

5
推荐指数
1
解决办法
2465
查看次数

子流程既输出到PIPE,又直接输出到stdout

我发现了许多看起来像我的问题,但是没有产生我可以使用的解决方案(最近的问题是:子进程输出到stdout和PIPE

问题:我想使用需要很长时间的子流程来启动流程。运行命令后,我需要解析stdout-output和stderr-output。

目前,我这样做如下:

p = subprocess.Popen( command_list, stdout=subprocess.PIPE, 
    stderr=subprocess.PIPE )
out, error_msg = p.communicate()
print out + "\n\n" + error_msg

#next comes code in which I check out and error_msg
Run Code Online (Sandbox Code Playgroud)

但是此方法的缺点是,用户在运行过程中看不到该过程的输出。仅在最后输出。

有没有一种方法可以在命令运行时打印输出(就像我给出的命令没有stdout / stderr = subprocess.PIPE一样),并且最终还是通过p.communicate输出?

注意:我目前正在使用python 2.5(使用此python版本的旧软件版本)进行开发。

python subprocess

5
推荐指数
1
解决办法
2389
查看次数

标签 统计

python ×8

subprocess ×7

stderr ×2

stdout ×2

iostream ×1

memory ×1

pexpect ×1

popen ×1

pty ×1

stream ×1

tty ×1