Python3 print flush buffer immediately
Python's standard out is buffered (meaning that it collects some of the data "written" to standard out before it writes it to the terminal).
Calling sys.stdout.flush() forces it to "flush" the buffer, meaning that it will write everything in the buffer to the terminal, even if normally it would wait before doing so.
method 1: (in each print, using "flush" keyword in 'print' function (since python 3.3)) print('', flush=True)
method 2: (change the default for the print function by using functools.partial on the global scope of a module) import functools print = functools.partial(print, flush=True) >>> print = functools.partial(print, flush=True) >>> print functools.partial(<built-in function print>, flush=True)
method 3: import sys sys.standout.flush()
Reference:
https://stackoverflow.com/questions/230751/how-to-flush-output-of-print-function/33265549
https://stackoverflow.com/questions/10019456/usage-of-sys-stdout-flush-method
posted on 2018-10-19 10:34 Quinn-Yann 阅读(524) 评论(0) 编辑 收藏 举报