This is possibly a nooby question but i cant find a way to do it! I need to clear the stdin buffer in python.
Imagine i have following bash script running:
i=0
for (( ; ; ))
do
echo "$i"
((i++))
done
run from the command line like so: ./loop.sh | python myProg.py
in myProg.py i wish to have something as follows:
count = 100
f = fileinput.input()
while True:
sleep(2)
# clear stdin somehow ...
# and read the most recent 100 lines
i = 0
while i < count:
myBuffer[i] = f.readline()
if len(myBuffer[i]) > 0:
i += 1
print myBuffer
i don't think i can just read all the lines, before it because it is spitting them out at a high rate and if sleep (just for testing atm) is a few minutes it seems silly ... is there a way to set the stdin buffer size in python? Or just truncate/clear it? Btw im using python 2 so no bufsize argument
I had a look here How to avoid Python fileinput buffering
Any python way of doing it? But ill try unbuffer too: https://unix.stackexchange.com/questions/25372/turn-off-buffering-in-pipe
Update: no luck with unbuffer or stdbuf ...