[IPython-dev] output from script is delayed

Gregor Thalhammer gregor.thalhammer at gmail.com
Thu Dec 12 13:07:02 EST 2013


Am 12.12.2013 um 15:12 schrieb Vasco Tenner <vasco+python at tenner.nl>:

> Dear all,
> 
> I encountered that the output from a script run by %run -i is not 
> directly displayed in the notebook.
> 
> When investigating I tested the following example:
> 
> import time
> for i in range(10):
>     print i
>     time.sleep(2)
> 
> In this example 0 and 1 are displayed at the same time, after 4 seconds!
> 
> In the script I run the first sys.stdout.write call does not output 
> anything, but after the second call both are displayed (the same for 3 
> and 4 etc). This can partialy overcome by adding writing an empty string 
> after each sys.stdout.write call:
> sys.stdout.write('foo')
> sys.stdout.write('')
> 
> However, still the first one is not displayed immediately.
> 
> Is this a flaw in the ipython notebook kernel/frontend communication or 
> do I miss something?
> 

In Python output to stdout is buffered, so you have to add a sys.stdout.flush() after the print to immediately show the output. 

Gregor

> 
> Vasco Tenner
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev




More information about the IPython-dev mailing list