subprocess.Popen instance hangs

Tim Johnson tim at akwebsoft.com
Thu Aug 29 18:56:21 EDT 2013


* MRAB <python at mrabarnett.plus.com> [130829 11:04]:
> On 29/08/2013 19:34, Tim Johnson wrote:
> >could use some examples.
> >
> The subprocess will terminate when it has finished writing its output,
> but because you're not consuming any of the output (you're waiting for
> it to finish), the buffer fills up and blocks the subprocess.
> 
> Try reading the output or using the .communicate method.
> 
> Alternatively, pass an open file as the stdout argument.
> 
  Kudos to all for the replies. Here is some code to review:
## execute process and read output  
p = subprocess.Popen(args,stderr=subprocess.STDOUT,stdout=subprocess.PIPE)
while 1 :
    output = p.stdout.read()
    if output :
        print(output)
    else : break

## Check for errors  
exit_status = p.wait()
if exit_status :
    if p.stderr :
        self.__err('Process terminated with exit status: %s' % (str(exit_status)),
                   'Following error message was found:'
                    p.stderr.read())
    else :
        self.__err('Process terminated with exit status: %s' % (str(exit_status)))

Without any error from the drush process, this works fine.

I can't quite figure out how to simulate/cause an error from drush,
so I would welcome comments on the error handling

thanks again
-- 
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