read a process output with subprocess.Popen

Nobody nobody at
Thu Feb 4 18:02:23 CET 2010

On Thu, 04 Feb 2010 04:28:20 -0800, Ashok Prabhu wrote:

> I m trying a read the output of a process which is running
> continuously with subprocess.Popen. However the readline() method
> hangs for the process to finish. Please let me know if the following
> code can be made to work with subprocess.Popen with threads or queues.
> I tried a lot of methods but to no avail. It would be great if someone
> can make it work.

This is an issue with grep, not Python per se.

By default, stdout (in the C library) is line-buffered if it refers to a
TTY, and block-buffered otherwise (e.g. if it refers to a pipe). grep
doesn't change the default buffering, so when it's stdout is a pipe, its
output is written in 4K blocks.

If you only need this to work with GNU grep, you can use the
--line-buffered switch to force stdout to be flushed after each line.

If it needs to be portable, you can implement the grep part in Python
(i.e. read p1.stdout and ignore any lines which don't contain a given
string or which don't match a given regex).

More information about the Python-list mailing list