python - Wrap subprocess' stdout/stderr -


i'd both capture , display output of process invoke through python's subprocess.

i thought pass file-like object named parameter stdout , stderr

i can see accesses filenoattribute - doing object. however, write() method never invoked. approach off or missing something?

class process(object):     class streamwrapper(object):         def __init__(self, stream):             self._stream = stream             self._buffer = []         def _print(self, msg):             print repr(self), msg         def __getattr__(self, name):             if not name in ['fileno']:                 self._print("# redirecting: %s" % name)             return getattr(self._stream, name)         def write(self, data):             print "###########"             self._buffer.append(data)             self._stream.write(data)             self._stream.flush()         def getbuffer(self):             return self._buffer[:]     def __init__(self, *args, **kwargs):         print ">> running `%s`" % " ".join(args[0])         self._stdout = self.streamwrapper(sys.stdout)         self._stderr = self.streamwrapper(sys.stderr)         kwargs.setdefault('stdout', self._stdout)         kwargs.setdefault('stderr', self._stderr)         self._process = subprocess.popen(*args, **kwargs)         self._process.communicate() 

update:

something i'd work well, ansi control characters move cursor , override output stuff. don't know whether correct term, here's example of meant: i'm trying automate git stuff , there have progress updates without writing new line each time.

update 2

it important me, output of subprocess displayed immediately. i've tried using subprocess.pipe capture output, , display manually, able display output, once process had completed. however, i'd see output in real-time.

stdin, stdout , stderr of process need real file descriptors. (that not restriction imposed python, rather how pipes work on os level.) need different solution.

if want track both stdout stderr in real time, need asynchronous i/o or threads.

  • asynchronous i/o: standard synchronous (=blocking) i/o, read 1 of streams block, disallowing access other 1 in real time. if on unix, can use non-blocking i/o described in this answer. however, on windows out of luck approach. more on asynchronous i/o in python , alternatives shown in this video.

  • threads: common way deal problem create 1 thread each file descriptor want read in real time. threads handle file descriptor assinged to, blocking i/o won't harm.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -