subprocess.Popen instance hangs

Tim Johnson tim at akwebsoft.com
Fri Aug 30 11:32:29 EDT 2013


* Nobody <nobody at nowhere.com> [130830 06:55]:
> On Thu, 29 Aug 2013 17:00:21 -0800, Tim Johnson wrote:
> 
> > ## This appears to be what works.
> >     def __exec(self,args) :
> >         """Run the process with arguments"""
> >        p =
> >        subprocess.Popen(args,stderr=subprocess.PIPE,stdout=subprocess.PIPE)
> >        while 1 :
> >            output = p.stdout.read()
> 
> If the process tries to write more than a pipe's worth of data to stderr,
> before closing stdout, it will block indefinitely.
> 
> If you want to process both stdout and stderr, you have to be able to
> consume the data in whatever order the process generates it, which means
> either using multiple threads or (on Unix) select/poll or non-blocking
> I/O. This is what the .communicate() method does (threads on Windows,
> select/poll on Unix).
> 
> The alternative is to merge both streams with stderr=subprocess.STDOUT, or
> redirect one of them to a file (or /dev/null, etc).
  In earlier code I, I was merging them...
  :) Like I said: gnarly! What if I were to do something like:
  ## code
  while 1: 
  	output = p.stout.read()
	err = p.stderr.read() ## trapping for AttributeError, etc..
	....
  ## /code
	break'ing if either no output or value in `err' 
 ?? 
  The objective is to display all output, but to also separate error
  messages from normal output.

  thank you
-- 
Tim 
tim at tee jay forty nine dot com or akwebsoft dot com
http://www.akwebsoft.com



More information about the Python-list mailing list