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

Chris Angelico rosuav at gmail.com
Thu Aug 22 11:33:12 EDT 2013


On Fri, Aug 23, 2013 at 1:26 AM, Ian Simcock
<Ian.Simcock at internode.on.net> wrote:
> Chris Angelico wrote:
>>
>> 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
>>
>
> If I run pip in the command window I can see it's output appearing line by
> line rather than on one block.
>
> I tried the code with the dir command but it's too fast for me to be sure if
> it's working or not.
>
> I tried again using the command "ping google.com" instead since I know that
> output's slowly and it something that everyone should have. In the command
> window I can see that the output appears over time, but from python I get
> nothing for a while and then suddenly get all the output in one rapid go.
>
>
> Can you think of anything else I can look at?

A lot of programs, when their output is not going to the console, will
buffer output. It's more efficient for many purposes. With Unix
utilities, there's often a parameter like --pipe or --unbuffered that
says "please produce output line by line", but Windows ping doesn't
have that - and so I'm seeing the same thing you are.

You should be able to see the time delay in dir by looking for some
particular directory name, and searching from the root directory.
Unless you're on a BLAZINGLY fast drive, that'll take Windows a good
while!

ChrisA



More information about the Python-list mailing list