I want to output some data from a Python script continuously to another program. As an example I will use cat, this is what currently happens:
If my test1.py script is like this:
print("Hello!")
when I run ./test1.py | cat the output is Hello!, it works because the script terminates immediately after execution.
The problem occurs when I have a script that writes continuously and never terminates like test2.py:
import time
a = 0
while True:
a += 1
print(a)
time.sleep(1)
Then ./test2.py | cat just hangs there because the script is not terminating.
I would like to send one number every second to cat and display it in real time, is it possible?
set flush=True in your print, your output is getting buffered, there is a nice artice Unix buffering delays output to stdout, ruins your day that explains what is going on:
import time
a = 0
while True:
a += 1
print(a, flush=True)
time.sleep(1)
If you are using python2 add from __future__ import print_function
You need to flush to the stdout after printing. So your code will look like this:
import sys
import time
a = 0
while True:
a += 1
print(a)
time.sleep(1)
sys.stdout.flush()
Now running python script.py | cat will print a.
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