我有下面的python脚本,我用它来尝试从一个没有适当换行符的程序中读取所有内容。这允许读取,而不必担心阻塞读取。然而,由于我对线程的了解不足,我怀疑这就是我的问题所在。
import subprocess
import shlex
import os
import time
from threading import Thread
import queue
class NonBlockingStreamReader:
def __init__(self, stream):
"""
:param stream: the stream to read from. Usually stdout or stderr.
"""
self._s = stream
self._q = queue.Queue()
def _populate_queue(_stream, _queue):
"""Collect lines from 'stream' and put them in 'queue'"""
while True:
_char = _stream.read(1)
if _char:
_queue.put(_char)
else:
raise UnexpectedEndOfStream
self._t = Thread(
target=_populate_queue,
args=(
self._s,
self._q
)
)
self._t.daemon = True
self._t.start() # Start collecting characters from the stream
def readchar(self, timeout=None):
try:
_tmp = self._q.get(block=timeout is not None, timeout=timeout)
return _tmp
except queue.Empty:
return None
class UnexpectedEndOfStream(Exception):
pass
def main():
proc = subprocess.Popen(
shlex.split('strace -o /home/arts/dlm/trace_output.txt stdbuf -o0 /home/arts/dlm/test'),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
nbsr = NonBlockingStreamReader(proc.stdout)
_data = b''
while True:
_char = nbsr.readchar(0.1)
if not _char:
break
else:
_data += _char
print(_data.decode())
proc.stdin.write(b'12345\n')
_data = b''
while True:
_char = nbsr.readchar(5)
if not _char:
break
else:
_data += _char
print(_data.decode())
print('Annnnd done.')
if __name__ == '__main__':
main()
下面是test
程序的预期输出:
Line 1 test
Line 2 test
Line 3 input: 12345 <--- input from user
Line 4 test: 12345
下面是strace的输出:
write(1, "Line 1 test", 11) = 11
write(1, "\n", 1) = 1
write(1, "Line 2 test", 11) = 11
write(1, "\n", 1) = 1
write(1, "Line 3 input: ", 14) = 14
fstat(0, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fb76716d000
read(0, "12345\n", 4096) = 6
write(1, "Line 4 test: 12345\n\n", 20) = 20
exit_group(1) = ?
这表明(至少对我而言)应用程序提供了请求的输出。除非我遗漏了什么,否则read(0, "12345\n", 4096)
会显示proc.stdin.write(b'12345\n')
。下一行显示它读回了我期望的输出。然而,实际的输出是:
Line 1 test
Line 2 test
Line 3 input:
Annnnd done.
如果我将一些打印语句放在_char = _stream.read(1)
后面,它什么也不会显示。如果我将它们添加到readchar
函数中,则会显示None
。
有些东西破坏了stdout,所以无论它去哪里,它都不会进入管道。有人能给我指引正确的方向吗?
发布于 2018-12-20 04:56:07
我想通了。需要将bufsize=0
作为arg转到Popen
。
https://stackoverflow.com/questions/53858892
复制相似问题