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

Ian Simcock Ian.Simcock at Internode.on.net
Fri Aug 23 03:01:40 EDT 2013


Rob Wolfe wrote:
> Ian Simcock <Ian.Simcock at Internode.on.net> writes:
>
> When file object is used in a for loop it works like an iterator
> and then it uses a hidden read-ahead buffer.
> It might cause this kind of blocking.
> You can read more details here (description of method ``next``):
> http://docs.python.org/lib/bltin-file-objects.html
>
> So basically non-blocking loop might look like this:
>
> while True:
>      line = p.stdout.readline()
>      if not line: break
>      print line
>
> HTH,
> Rob
>

Thanks, but some further research seems to indicate that the problem is 
that the standard C libraries are probably buffering the output when the 
it's being redirected, so the problem is coming from the command line 
tool rather than the python code.

Ian Simcock.



More information about the Python-list mailing list