Enthought Canopy doesn't print right away when statement occurs

纵然是瞬间 提交于 2019-11-28 05:14:51

问题


A while ago I switched from Enthought's old EPD to their newer Canopy system. For the most part it's nice, but one aspect has been particularly vexing.

Whenever I run a python script, either from within the Canopy iPython environment or from the command line, none of my print statements actually get printed right away when that part of the script is hit. Instead, multiple prints seem to get executed all at once at a later time.

As an example...

import numpy as np

print "About to start long computation..."
a = np.random.randn(1e8)
print "Computation finished."

doesn't print the first statement until after a is finished being generated, when both statements are printed simultaneously. (You can tell when the calculation is occurring by watching the CPU monitor.)

Does anyone know what's going on here? If relevant, I'm running Canopy 1.0.0.1160, with Python 2.7.3 64bit on a Windows 7 machine.


回答1:


No, this is not a change between EPD and Canopy. While I suppose there might be some python distributions which default to buffering off, EPD was not one of them -- the performance hit could have been too severe (as kindall's comment mentions.) Better to let the programmer decide when it's important for the user to see console output immediately (typically for status updates).

BTW, IPython in the Canopy GUI is simply IPython QtConsole. If you are depending on console I/O, you may also need to be aware of this longstanding issue with QtConsole:

I don't think there's a reasonable workaround for Canopy IPython, other than do it "properly", i.e. with flush.

https://support.enthought.com/entries/22157050-Canopy-Python-prompt-QtConsole-Can-t-run-interactive-OS-shell-commands




回答2:


This looks like buffered output. Try running your script as:

python -u yourscript

The -u flag turns buffering off.

(Replace python by your OS's python executable's name.)



来源:https://stackoverflow.com/questions/18709659/enthought-canopy-doesnt-print-right-away-when-statement-occurs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!