Created
February 20, 2014 03:59
-
-
Save yhchan/9106875 to your computer and use it in GitHub Desktop.
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 subprocess | |
import select | |
from threading import Thread | |
from cStringIO import StringIO | |
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()) | |
def execute_cmd(command): | |
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() | |
return (p, cmd_stdout, cmd_stderr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment