Running a command line program and reading the result as it runs

Chris Angelico rosuav at gmail.com
Thu Aug 22 02:22:19 EDT 2013


On Thu, Aug 22, 2013 at 3:51 PM, Ian Simcock
<Ian.Simcock at internode.on.net> wrote:
> When I use this code I can see that the Popen works, any code between the
> Popen and the for will run straight away, but as soon as it gets to the for
> and tries to read p.stdout the code blocks until the command line program
> completes, then all of the lines are returned.
>
> Does anyone know how to get the results of the program without it blocking?

Is the program actually producing output progressively? I just tried
your exact code with "dir /ad /s /b" and it worked fine, producing
output while the dir was still spinning (obviously setting shell=True
to make that work, but I don't think that'll make a difference). It
may be that pip buffers its output. Is there a parameter to pip to
make it pipe-compatible?

ChrisA



More information about the Python-list mailing list