Skip to content

Instantly share code, notes, and snippets.

@yhchan
Last active December 17, 2015 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yhchan/5618658 to your computer and use it in GitHub Desktop.
Save yhchan/5618658 to your computer and use it in GitHub Desktop.
subprocess with reading stdout and stderr in threads
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