Why does python keep buffering stdout even when flushing and using -u?
问题 $ cat script.py import sys for line in sys.stdin: sys.stdout.write(line) sys.stdout.flush() $ cat script.py - | python -u script.py The output is right but it only starts printing once I hit Ctrl-D whereas the following starts printing right away : $ cat script.py - | cat which led me to think that the buffering does not come from cat. I managed to get it working by doing : for line in iter(sys.stdin.readline, ""): as explained here : Streaming pipes in Python, but I don't understand why the