반응형
질문
I'm using the subprocess module to start a subprocess and connect to its output stream (standard output). I want to be able to execute non-blocking reads on its standard output. Is there a way to make .readline non-blocking or to check if there is data on the stream before I invoke .readline
? I'd like this to be portable or at least work under Windows and Linux.
Here is how I do it for now (it's blocking on the .readline
if no data is available):
p = subprocess.Popen('myprogram.exe', stdout = subprocess.PIPE)
output_str = p.stdout.readline()
답변
fcntl
, select
, asyncproc
이 경우에는 도움이 되지 않습니다.
운영 체제에 관계없이 블로킹 없이 스트림을 읽는 신뢰할 수 있는 방법은 Queue.get_nowait()
를 사용하는 것입니다:
import sys
from subprocess import PIPE, Popen
from threading import Thread
try:
from queue import Queue, Empty
except ImportError:
from Queue import Queue, Empty # python 2.x
ON_POSIX = 'posix' in sys.builtin_module_names
def enqueue_output(out, queue):
for line in iter(out.readline, b''):
queue.put(line)
out.close()
p = Popen(['myprogram.exe'], 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()
# ... 다른 작업을 여기서 수행합니다.
# 블로킹 없이 라인 읽기
try: line = q.get_nowait() # or q.get(timeout=.1)
except Empty:
print('아직 출력 없음')
else: # 라인을 받음
# ... 라인으로 무언가를 수행합니다.
반응형
댓글