我在这里看到过这个问题,但给出的答案在我的情况下不起作用,并被标记为重复。如何避免在Python文件输入库中缓冲
python -u
does not work forstdin
in Python 3.sys.stdin = sys.stdin.detach()
抛出一个ValueError: underlying buffer has been detached
。- 这些都不适用于非
stdin
输入和其他文件被用作流。 - Adding a hook does not work:
FileInput(openhook=hook_nobuf)
and usingopen(buffering=0)
in the hook.
我挖在源代码(/usr/lib/python3.2/fileinput.py
),看到readlines(bufsize)
正在内部用于装载的缓冲器。没有外壳或其他管道shenanigans。
实际上,我认为你可能希望'python -u'在你需要的任何东西之上。你想删除'stdin'上任何基础的Python和/或-stdio缓冲区,并且_also_删除任何更高级的行读缓冲区,对吧? – abarnert 2013-02-21 21:30:08