私はcat
でサブプロセスを使って読んでいる100行のサンプルファイルを持っています。ただし、キュー内の出力は常に切り捨てられます。私はそれがパイプを検出するので、その出力をバッファリングするcat
のためかもしれないと思う。プロセス出力のバッファリングによる切り捨てが発生しましたか?
def StdOutThread():
while not p.stdout.closed and running:
line = ""
while not line or line[-1] != "\n":
r = p.stdout.read(1)
if not r:
break
line += r
pending_line["line"] = line
if line and line[-1] == "\n":
line = line[:-1]
if line:
queue.put(("out", line))
これらのスレッドが開始され、それらがキューに読んだダンプ:
p = subprocess.Popen("cat file.txt",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
shell=True,
bufsize=0)
私は
cat
のstdoutとstderrパイプから読み取るために別のスレッドを使用しています。
cat
が生存している間、メインスレッドはこのキューから読み取ります。
with CancelFunction(p.kill):
try:
stdout_thread = threading.Thread(target=StdOutThread)
stdout_thread.start()
while p.poll() is None:
ReadFromQueue()
while not queue.empty():
ReadFromQueue()
finally:
running = False
stdout_thread.join()
私は、この問題を克服するためにpexpectを使用して考えられますが、同時にもpexpectで可能ていないようだstdoutとstderrを区別したいしています。ヘルプは非常に高く評価されます。