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:python
https://stackoverflow.com/questions/230751/how-to-flush-output-of-print-function/33265549ui
https://stackoverflow.com/questions/10019456/usage-of-sys-stdout-flush-methodspa