【发布时间】:2021-03-30 02:15:18
【问题描述】:
如何在不等待的情况下读取管道的缓冲区。子进程正在执行 Swift 脚本。如果它是一个 python 脚本,则有一个用于使管道无缓冲 (-u) 的 python 标志。还有什么办法可以解决吗?
sub_proc = subprocess.Popen(['swift', 'script2.swift'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
thr1 = threading.Thread(target=self.pipe_reader, args=[sub_proc.stdout]).start()
thr2 = threading.Thread(target=self.pipe_reader, args=[sub_proc.stderr]).start()
def pipe_reader(self, pipe):
for line in iter(pipe.readline, b''):
self.q.put((pipe, line))
self.q.put((pipe, None))
【问题讨论】:
标签: python subprocess pipe buffer