Last active
December 17, 2015 13:39
-
-
Save yhchan/5618658 to your computer and use it in GitHub Desktop.
subprocess with reading stdout and stderr in threads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import select | |
import subprocess | |
from threading import Thread | |
from cStringIO import StringIO | |
import logging | |
logger = logging.getLogger(__name__) | |
def read_stream(stream, output_stream, log_func): | |
while True: | |
r, w, x = select.select([stream.fileno()], [], [], 1.0) | |
if not r: | |
continue | |
line = stream.readline() | |
if not line: | |
break | |
output_stream.write(line) | |
output_stream.flush() | |
log_func(line.strip()) | |
p = subprocess.Popen(command, shell=True, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
cmd_stdout = StringIO() | |
cmd_stderr = StringIO() | |
Thread(target=read_stream, | |
args=(p.stdout, cmd_stdout, logger.info)).start() | |
Thread(target=read_stream, | |
args=(p.stderr, cmd_stderr, logger.error)).start() | |
p.wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment