Given the following program in Python:
import sys
print("input")
while True:
pass
and the equivalent in C:
#include <stdio.h>
int main() {
printf("input\n");
while(1);
return 0;
}
When I pipe the program with cat, the output is by default buffered (instead of line-buffered). As a consequence, I have no output:
% python3 ./a.py | cat
(empty)
% ./a.out | cat
(empty)
I can use stdbuf to switch back to line-buffering:
% stdbuf -oL ./a.out | cat
input
But that doesn't work with Python:
% stdbuf -oL python3 a.py | cat
(empty)
Any idea why? I'm aware of the existence of python3 -u but I want line-buffering, not "no buffering" and I want this command line to work for other languages as well. The command unbuffer seems to work too, but I'd like to understand why stdbuf doesn't work here.
pythonis doing this. - Eugene Sh.