我正在尝试创建一个流对象,只要数据写入它就会触发回调函数。
class MonitoredStream():
def __init__(self, outstream, callback):
self.outstream = outstream
self.callback = callback
def write(self, s):
self.callback(s)
self.outstream.write(s)
def __getattr__(self, attr):
return getattr(self.outstream, attr)
当我直接调用write方法时,这很好用,但是当我将一个子进程'输出连接到流时,我也希望它能工作。例如:
def f(s):
print("Write")
p = sub.Popen(["sh", "test.sh"], stdout=MonitoredStream(sys.stdout, f))
p.communicate()
这只是将输出直接发送到sys.stdout,完全绕过write函数。有没有办法可以监控这个输出呢?
答案 0 :(得分:2)
我认为这里的问题是subprocess.Popen
不使用Python接口到管道 - 而是获取文件描述符,然后使用它直接写入管道,当你给出属性时stdout
管道,意味着它使用它,绕过你的代码。
我最好的解决方法是在中间设置一个新的中间管道,让你自己处理流。我会将其实现为上下文管理器:
import sys
import os
from subprocess import Popen
from contextlib import contextmanager
@contextmanager
def monitor(stream, callback):
read, write = os.pipe()
yield write
os.close(write)
with os.fdopen(read) as f:
for line in f:
callback(line)
stream.write(line)
def f(s):
print("Write")
with monitor(sys.stdout, f) as stream:
p = Popen(["ls"], stdout=stream)
p.communicate()
虽然你当然可以使用一个类:
import sys
import os
from subprocess import Popen
class MonitoredStream():
def __init__(self, stream, callback):
self.stream = stream
self.callback = callback
self._read, self._write = os.pipe()
def fileno(self):
return self._write
def process(self):
os.close(self._write)
with os.fdopen(self._read) as f:
for line in f:
self.callback(line)
self.stream.write(line)
def f(s):
print("Write")
stream = MonitoredStream(sys.stdout, f)
p = Popen(["ls"], stdout=stream)
p.communicate()
print(stream.process())
虽然我觉得这不太优雅。