I wrote this article because I sometimes forget it and see painful eyes.
If you run it as-is in the shell, the following command should work as expected, as it says. Output 1 and wait 3 seconds, then output 2.
$ python -c 'import time; print(1); time.sleep(3); print(2)'
1
2
$
If you pass a pipe through this, it will not come out unless the buffer size of the pipe is accumulated.
In the above example,
$ python -c 'import time; print(1); time.sleep(3); print(2)' | cat
1
2
$
I just piped it to cat
, but the lines 1 and 2 come out at the same timing after 3 seconds.
To put it like that, the one who says "the pipe is clogged". Even if it is written to a file, it is also written with a delay.
If you want to suppress this behavior, use the `` `-u``` option.
$ python -u -c 'import time; print(1); time.sleep(3); print(2)' | cat
1
2
$
↑ Wait 3 seconds after 1 comes out, then 2 comes out.
http://docs.python.jp/3/using/cmdline.html#cmdoption-u
pythonunbuffered
You can get the same effect by putting a non-empty string in the environment variable.
Recommended Posts