この質問のコードにバリアントを実装しました。
Python の subprocess.PIPE でのノンブロッキング読み取り
このダミー プログラムからリアルタイムで出力を読み取ってみるには、次のようにしtest.py
ます。
import time, sys
print "Hello there"
for i in range(100):
time.sleep(0.1)
sys.stdout.write("\r%d"%i)
sys.stdout.flush()
print
print "Go now or I shall taunt you once again!"
もう 1 つの質問のバリエーションは、ダミー プログラムtest.py
が\r
. だからここにあります:
import sys,time
from subprocess import PIPE, Popen
from threading import Thread
try:
from Queue import Queue, Empty
except ImportError:
from queue import Queue, Empty # Python 3.x
ON_POSIX = 'posix' in sys.builtin_module_names
def enqueue_output(out, queue):
while True:
buffersize = 1
data = out.read(buffersize)
if not data:
break
queue.put(data)
out.close()
p = Popen(sys.executable + " test.py", stdout=PIPE, bufsize=1, close_fds=ON_POSIX)
q = Queue()
t = Thread(target=enqueue_output, args=(p.stdout, q))
t.daemon = True # Thread dies with the program
t.start()
while True:
p.poll()
if p.returncode:
break
# Read line without blocking
try:
char = q.get_nowait()
time.sleep(0.1)
except Empty:
pass
else: # Got line
sys.stdout.write(char)
sys.stdout.flush()
print "left loop"
sys.exit(0)
これには2つの問題があります
- 終了することはありません-
p.returncode
値を返すことはなく、ループは終了しません。どうすれば修正できますか? - 本当に遅いです!増やさずに効率化する方法はあり
buffersize
ますか?