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

 
 
 
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/quinn-yann/p/9814995.html