Skip to content

Instantly share code, notes, and snippets.

@AlexanderWingard
Created May 26, 2015 11:09
Show Gist options
  • Save AlexanderWingard/2e52b5c7f04df937fce8 to your computer and use it in GitHub Desktop.
Save AlexanderWingard/2e52b5c7f04df937fce8 to your computer and use it in GitHub Desktop.
import logging
import subprocess
import shlex
from subprocess import Popen, PIPE
from threading import Thread
from Queue import Queue, Empty
logging.basicConfig(format="%(asctime)s %(out)3s %(prog)8s | %(message)s", level=logging.DEBUG)
def c(str, *args):
jobs = Queue()
def line_reader(pipe, result):
jobs.get()
for line in iter(pipe.readline, b''):
logging.info(line.rstrip(), extra={'prog':str, 'out':pipe.fileno()})
result.append(line)
jobs.task_done()
def pipe_watch(pipe):
jobs.put(1)
result = []
t = Thread(target=line_reader, args=[pipe, result])
t.daemon = True
t.start()
return result
p = subprocess.Popen(shlex.split(str.format(*args)),
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
close_fds=True)
outres = pipe_watch(p.stdout)
errres = pipe_watch(p.stderr)
p.wait()
jobs.join()
return (p.returncode, outres, errres)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment