Unbuffered reading from stdin in python

I am writing a python script that can read input through a pipe from another command, for example

batch_job | myparser 

My script myparser processes the output of batch_job and writes it to its own stdout. My problem is that I want to see the result immediately (the batch_job output is processed in turn), but it looks like this infamous stdin buffering (supposedly 4 KB, I did not confirm), which delays everything.

The problem has already been discussed here and here .

I tried the following:

  • open stdin with os.fdopen(sys.stdin.fileno(), 'r', 0)
  • using -u in my hastbang: #!/usr/bin/python -u
  • export PYHTONUNBUFFERED=1 before calling the script
  • clearing my output after every line I read (in case the problem arose from output buffering rather than input buffering).

My python version is 2.4.3. I have no way to update or install any additional programs or packages. How can I get rid of these delays?

+6
source share

All Articles