8
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?

hd810
  • 197
  • 1
  • 2
  • 8
  • the notebook must wait for the code to finish before you see the output – Padraic Cunningham Oct 13 '14 at 20:52
  • 1
    @PadraicCunningham: you don't need to wait for the script to finish before you can see the output. See [my answer](http://stackoverflow.com/a/26352157/4279) – jfs Oct 14 '14 at 03:31

1 Answers1

15

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())?

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • I was having the problem with displaying output from python script gitlab deployment task and adding `sys.stdout.flush()` after each print command solved my issue. Now the output prints out as the script executes. – To Ka Nov 13 '17 at 06:55