Unable to pipe python output to program
<p>I want to pipe stdout from python to another program, but I am faceing a problem where my stdout is not piped.</p>

<p>I have it shortened down to a simple sample:</p>

<pre><code>import time

while True:
    print("Hello!")
    time.sleep(1)
</code></pre>

<p>I check it with cat like so:</p>

<pre><code>./my_python_script.py | cat
</code></pre>

<p>And then I get nothing at all.</p>

<p>Strange thing is that it works just fine if i remove the sleep command. However, I do not want to pipe the output that fast, so I would really like to sleep for a second.</p>

<p>I checked with the corresponding bash script:</p>

<pre><code>while true; do
    echo "Hello!"
    sleep 1
done
</code></pre>

<p>And that works like a charm too. So any idea as to why the python script does not pipe the output as expected? Thanks!</p>

<p>You'll need to flush the stdout:</p>

<pre><code>import time
import sys

while True:
    print("Hello!")
    sys.stdout.flush()
    time.sleep(1)
</code></pre>

python linux