Frâncio Rodrigues
Frâncio Rodrigues

Reputation: 2290

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.

Upvotes: 2

Views: 1286

Answers (1)

Michael Dillon
Michael Dillon

Reputation: 32392

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/

Upvotes: 2

Related Questions