简体   繁体   中英

How to interrupt an infinite loop on Python under the Enthought Canopy enviroment?

I do not know how to interrupt an infinite loop on python 2.7 running through Canopy on a Windows 7 OS. Ctrl+C doesn't work, "Interrupt Kernel" under the "Run" menu also never seems to work. On linux (ubuntu) the keyboardinterrupt ctrl+c works just fine but not on Windows.

The only way I've been able to stop a accidental infinite loop is through the "Restart Kernel" option under the "Run" menu. Unfortunately that means that I loose all my working variables, which is undesirable. On MATLAB Ctrl+C worked just fine.

I've found several similar questions of keyboardinterrupt issues on Python but none using the Enthought Canopy environment.

Any help is much appreciated.

In general, Ctrl-C will only work to break an infinite loop if code in the loop call OS services. Given your comment about MATLAB, I suspect that your loop is compute-only. An easy fix to this is to check for a keypress inside the loop which would also allow you to define your own interrupt code. There is some nice code on Activestate which does this for both Windows or Unix-like OSes http://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM