python how to use subprocess pipe with linux shell
<p>I have a python script search for logs, it continuously output the logs found and I want to use linux pipe to filter the desired output. example like that:</p>

<p>$python logsearch.py | grep timeout</p>

<p>The problem is the sort and wc are blocked until the logsearch.py finishes, while the logsearch.py will continuous output the result.</p>

<p>sample logsearch.py:</p>

<pre><code>p = subprocess.Popen("ping google.com", shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
for line in p.stdout:
    print(line)
</code></pre>

<p><strong>UPDATE:</strong></p>

<p>figured out, just change the stdout in subprocess to sys.stdout, python will handle the pipe for you.</p>

<pre><code>p = subprocess.Popen("ping -c 5 google.com", shell=True, stdout=**sys.stdout**)
</code></pre>

<p>Thanks for all of you help!</p>

<p>And why use grep? Why don't do all the stuff in Python?</p>

<pre><code>from subprocess import Popen, PIPE
p = Popen(['ping', 'google.com'], shell=False, stdin=PIPE, stdout=PIPE)

for line in p.stdout:
    if 'timeout' in line.split():
        # Process the error
        print("Timeout error!!")
    else:
        print(line)
</code></pre>

<p><strong>UPDATE:</strong><br>
I change the Popen line as recommended @triplee. Pros and cons in  <a href="http://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess">Actual meaning of &#39;shell=True&#39; in subprocess</a> </p>

python linux shell pipe subprocess