gpt4 book ai didi

python - 合并并同步 stdout 和 stderr?

转载 作者:太空狗 更新时间:2023-10-30 03:07:21 26 4
gpt4 key购买 nike

假设我正在使用 python 脚本运行 exe:

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

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

有什么方法可以在其上生成一个包含 stdout 和 stderr 的合并和同步文本文件吗?它应该用时间和来源(我们的/错误的)格式化。

谢谢

最佳答案

这有点棘手,因为您需要在子进程运行时轮询其 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

示例输出:

stdout 2011-03-03T19:30:44.838145: .:
stdout 2011-03-03T19:30:44.838145: total 0
stdout 2011-03-03T19:30:44.838338: -rw-r--r-- 1 pat pat 0 2011-03-03 19:30 bar
stdout 2011-03-03T19:30:44.838518: -rw-r--r-- 1 pat pat 0 2011-03-03 19:30 foo
----------------------------------------

stderr 2011-03-03T19:30:44.837189: ls: cannot access xyzabc: No such file or directory
----------------------------------------

stderr 2011-03-03T19:30:44.837189: ls: cannot access xyzabc: No such file or directory
stdout 2011-03-03T19:30:44.838145: .:
stdout 2011-03-03T19:30:44.838145: total 0
stdout 2011-03-03T19:30:44.838338: -rw-r--r-- 1 pat pat 0 2011-03-03 19:30 bar
stdout 2011-03-03T19:30:44.838518: -rw-r--r-- 1 pat pat 0 2011-03-03 19:30 foo
----------------------------------------

关于python - 合并并同步 stdout 和 stderr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4984549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com