To check the progress of heavy processing, the print statement inserted in the middle of processing is not output at that time It may come out all at once after the processing is completed.
This behavior may be correct in terms of processing speed, but This is a problem when you want it to be released immediately. ..
In the example sentence below, it happens with jupyter (IPython notebook), so it may depend on the buffer settings. On the console, even if flush was not specified, it was output sequentially.
python3.3 or later
print("", flush=True)
python3.Before 3(The code below is python2 series)
import sys
print ""
sys.stdout.flush()
** Example: with python3.4 and jupyter **
Confirmed with jupyter (On the console, it was output sequentially even without flushing ...)
import time
time.sleep(1)
print("processing A...")#Not displayed here ...
time.sleep(1)
print("finish!")#It is output together with the above print. .. ..
#If you use flush ...
time.sleep(1)
print("processing B(flush=True)", flush=True)#Will be displayed soon!
time.sleep(1)
print("finish!", flush=True)
Recommended Posts