How can I read streaming output of a subprocess
Damjan Georgievski
gdamjan at gmail.com
Wed May 2 10:51:13 EDT 2012
>> I want to read the stream of an external process that I start with Python.
>> From what I've seen that's not possible with the subprocess module?
>>
>> I want to read the output of "ip monitor neigh" which will show changes in
>> the ARP table on my Linux computer. Each change is a new line printed by
>> "ip" and the process continues to run forever.
>>
>
> You can use subprocess for this. If you don't call wait() or
> communicate() on the Popen object, the process will happily continue
> running. You can call readline on the stdout pipe, which will block
> until the command you are running outputs (and flushes) a line.
>
> Something like this should work:
>
> p = subprocess.Popen(['ip', 'monitor', 'neigh'], stdout=subprocess.PIPE)
> first_line = p.stdout.readline()
>
> Buffering done by "ip" can ruin your day. It seems reasonable to
> expect, though, that something that is printing state changes on
> stdout will be careful to flush its output when appropriate.
>
thanks,
With your confirmation, I've rechecked again and now I see that the
problem I experienced before was with some strange ssh interaction.
More information about the Python-list
mailing list