subprocess.Popen instance hangs

Nobody nobody at nowhere.com
Fri Aug 30 10:47:27 EDT 2013


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).




More information about the Python-list mailing list