I'm writing a python script that can read input through a pipe from another command like so
batch_job | myparser
My script myparser
processes the output of batch_job
and write to its own stdout. My problem is that I want to see the output immediately (the output of batch_job is processed line-by-line) but there appears to be this notorious stdin buffering (allegedly 4KB, I haven't verified) which delays everything.
The problem has been discussed already here here and here.
I tried the following:
- open stdin using
os.fdopen(sys.stdin.fileno(), 'r', 0)
- using
-u
in my hashbang:#!/usr/bin/python -u
- setting
export PYTHONUNBUFFERED=1
right before calling the script - flushing my output after each line that was read (just in case the problem was coming from output buffering rather than input buffering)
My python version is 2.4.3 - I have no possibility of upgrading or installing any additional programs or packages. How can I get rid of these delays?
batch_job
from within yourmyparser
as a subprocess and then you get to fully control STDOUT/STDIN? The way you have it set up doesn't depend only on Python but also on shell buffering itself. – Vivbatch_job
. Have you tried to run it withstdbuf -o0 -e0
as suggested in the question you've linked (unix.stackexchange.com/a/25378)? – Ironsmith