I want to pipe stdout from python to another program, but I am faceing a problem where my stdout is not piped.
I have it shortened down to a simple sample:
import time
while True:
    print("Hello!")
    time.sleep(1)
I check it with cat like so:
./my_python_script.py | cat
And then I get nothing at all.
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.
I checked with the corresponding bash script:
while true; do
    echo "Hello!"
    sleep 1
done
And that works like a charm too. So any idea as to why the python script does not pipe the output as expected? Thanks!
You'll need to flush the stdout:
import time
import sys
while True:
    print("Hello!")
    sys.stdout.flush()
    time.sleep(1)
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With