Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ipython notebook is NOT printing until the whole program is finished

import time

print 1
time.sleep(5)

I ran the code above in IPython notebook and normal script separately.

In IPython Notebook, it doesn't print the number '1' until time.sleep(5) is finished, while in normal script it DOES print out number '1' first and go into time.sleep(5). What would that happen?

This example is just to illustrate my problem: I use print to print out some text at every stage in my code, which takes a long time to finish, so that I can know where the programme has got to. I found this works fine when executing the script, but in IPython Notebook print often lags behind and everything is printed out when the whole program is finished.

Is there any way to solve this in IPython Notebook?

like image 281
hd810 Avatar asked Oct 13 '14 20:10

hd810


1 Answers

It is a buffering issue. Add sys.stdout.flush() after the print, to see the output immediately. You could also use python -u command or set PYTHONUNBUFFERED envvar. See Disable output buffering.

python uses line-buffering if it is run interactively (in a terminal) and block buffering (e.g., ~4K bytes buffers) if the output is redirected. Compare:

$ python your_script.py

And

$ python your_script.py | cat

The output is delayed in the second case.

See the second reason in Q: Why not just use a pipe (popen())?

like image 125
jfs Avatar answered Sep 28 '22 03:09

jfs