问题
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?
回答1:
In Linux, bash, what you are looking for seems to be the stdbuf command.
If you want no buffering (i.e. an unbuffered stream), try this,
# batch_job | stdbuf -o0 myparser
If you want line buffering, try this,
# batch_job | stdbuf -oL myparser
来源:https://stackoverflow.com/questions/33305131/unbuffered-read-from-stdin-in-python