unbuffered read from stdin in python

[亡魂溺海] 提交于 2020-01-24 02:34:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!