A program continuously prints to standard output one line at a time.
I am trying to read and process one line at a time of this input without having to wait for the program to complete.
As an example, the below writeOutput.py writes one line at a time to stdout (waiting between each line between 1 and 3 seconds).
Calling ./writeOutput.py | ./processEachLine.py
requires writeOutput.py to complete before processEachLine.py is able to start processing the first line.
Is there anyway to achieve this in python? Even by calling writeOutput.py directely within the python program instead of using a pipe?
Any help would be highly appreciated.
writeOutput.py
#!/usr/bin/env python
import random
import time
i = 0
while i < 5:
n = int(1 + (random.random()*10) % 3)
i += 1
time.sleep(n)
print(str(n) + " test")
processEachLine.py
#!/usr/bin/env python
import sys
while 1:
line = sys.stdin.readline()
if not line:
break
print(">>" + line)