반응형
다음은 지금 수행하는 방법입니다 (사용 가능한 데이터가없는 경우 .readline
에서 차단됨).
p = subprocess.Popen('myprogram.exe', stdout = subprocess.PIPE)
output_str = p.stdout.readline()
해결 방법
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()
# ... do other things here
# read line without blocking
try: line = q.get_nowait() # or q.get(timeout=.1)
except Empty:
print('no output yet')
else: # got line
# ... do something with line
참조 페이지 https://stackoverflow.com/questions/375427
반응형
'파이썬' 카테고리의 다른 글
파이썬 Django에서 이메일 전송 테스트 (0) | 2020.11.03 |
---|---|
파이썬 Titlecasing a string with exceptions (0) | 2020.11.03 |
파이썬 re.finditer와 re.findall의 다른 동작 (0) | 2020.11.03 |
파이썬 Selecting columns with condition on Pandas DataFrame (0) | 2020.11.03 |
파이썬 'double_scalars에서 잘못된 값이 발견되었습니다'경고, 아마도 numpy (0) | 2020.11.03 |
댓글