read a process output with subprocess.Popen

Ashok Prabhu ashokprabhuv at gmail.com
Thu Feb 4 13:28:20 CET 2010


Hi,

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.

import subprocess

p1 = subprocess.Popen('tail -f /var/log/
messages',stdout=subprocess.PIPE,shell=True)
p2 = subprocess.Popen('grep
something',stdin=p1.stdout,stdout=subprocess.PIPE,shell=True)

while 1:
    line = p2.stdout.readline()
    print line

Thanks,
~Ashok.



More information about the Python-list mailing list