1
import time 
for i in range(100):
     time.sleep(0.1)
     print("sleeping is happening")

I think it will print the string every 0.1 seconds, but actually it prints the string 100 times on the output client after the code is done.

The answer is a little different (python version 3.5):

following is not useful

import sys
sys.stdout.flush() 

and

print("sleeping is happening", flush = True)

is corret.

Carl
  • 11
  • 3
  • Need more details – Sushant Jul 30 '18 at 11:13
  • do you need to flush the print calls perhaps – Chris_Rands Jul 30 '18 at 11:14
  • [How to flush output of print function](https://stackoverflow.com/questions/230751/how-to-flush-output-of-print-function) There is a solution in this question . but not the top one . ```print("sleeping is continuing", flush=True)``` – Carl Jul 30 '18 at 11:24
  • Welcome to Stackoverflow! Did the answers in the question above answer your problem? – Maximilian Peters Jul 30 '18 at 11:25
  • The solution is about **flush**, thank you very much!!! ```print("sleeping is continuing", flush=True)``` is the right answer. but I don't understand why there is no need in jupyter notebook... – Carl Jul 30 '18 at 11:28
  • 1
    Because Jupyter internally uses unbuffered output. You can also set the `PYTHONUNBUFFERED=1` environment variable to always have your stdout unbuffered, but there are performance implications. – AKX Jul 30 '18 at 11:37
  • Thank you for your answer. There so many things to learn..... – Carl Jul 30 '18 at 11:57

0 Answers0