How can I read streaming output of a subprocess
Kushal Kumaran
kushal.kumaran+python at gmail.com
Wed May 2 07:34:39 EDT 2012
On Wed, May 2, 2012 at 4:38 PM, Damjan Georgievski <gdamjan at gmail.com> wrote:
> 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.
--
regards,
kushal
More information about the Python-list
mailing list