合并并同步stdout和stderr?

2024-06-17 11:29:37 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我从python脚本运行一个exe,使用:

subprocess.call(cmdArgs,stdout=outf, stderr=errf)

当outf和errf是文本文件的文件描述符时。在

有什么方法可以在上面生成stdout和stderr的合并和同步文本文件吗? 它应该用时间和来源(our/err)格式化。在

谢谢


Tags: 文件方法脚本stderrstdout时间来源call
2条回答

您可以合并它们,并将subprocess.STDOUT作为subprocess.Popenstderr参数,但我不知道它们是否会用时间和源格式化。在

这有点棘手,因为您需要在子进程运行时轮询它的stdout和stderr文件描述符,以获得准确的时间戳。您还需要将输出切分为一个行列表,这样最终的结果可以很容易地合并和排序。你可以很容易地在阅读时合并这两个流,但这不是问题的一部分。在

我写得很快,但可以写得更干净、更紧凑:

import datetime
import os
import select
import subprocess

class Stream(object):

    def __init__(self, name, impl):
        self._name = name
        self._impl = impl
        self._buf = ''
        self._rows = []

    def fileno(self):
        "Pass-through for file descriptor."
        return self._impl.fileno()

    def read(self, drain=0):
        "Read from the file descriptor. If 'drain' set, read until EOF."
        while self._read() is not None:
            if not drain:
                break

    def _read(self):
        "Read from the file descriptor"
        fd = self.fileno()
        buf = os.read(fd, 4096)
        if not buf:
            return None
        if '\n' not in buf:
            self._buf += buf
            return []

        # prepend any data previously read, then split into lines and format
        buf = self._buf + buf
        tmp, rest = buf.rsplit('\n', 1)
        self._buf = rest
        now = datetime.datetime.now().isoformat()
        rows = tmp.split('\n')
        self._rows += [(now, '%s %s: %s' % (self._name, now, r)) for r in rows]

def run(cmd, timeout=0.1):
    """
    Run a command, read stdout and stderr, prefix with timestamp, and
    return a dict containing stdout, stderr and merged.
    """
    PIPE = subprocess.PIPE
    proc = subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE)
    streams = [
        Stream('stdout', proc.stdout),
        Stream('stderr', proc.stderr)
        ]
    def _process(drain=0):
        res = select.select(streams, [], [], timeout)
        for stream in res[0]:
            stream.read(drain)

    while proc.returncode is None:
        proc.poll()
        _process()
    _process(drain=1)

    # collect results, merge and return
    result = {}
    temp = []
    for stream in streams:
        rows = stream._rows
        temp += rows
        result[stream._name] = [r[1] for r in rows]
    temp.sort()
    result['merged'] = [r[1] for r in temp]
    return result

res = run(['ls', '-l', '.', 'xyzabc'])
for key in ('stdout', 'stderr', 'merged'):
    print 
    print '\n'.join(res[key])
    print '-'*40

输出示例:

^{pr2}$

相关问题 更多 >