KeyboardInterrupt unpredictable in Python 2.7 unde

2019-07-17 09:03发布

问题:

I'm writing python code to do numerical analysis, and I've been using ipython or ipython -pylab as my command line interface. I often run into situations where some code is taking for-freaking-ever to run, and I need to stop it. However, Ctrl-C is problematic; sometimes it works, sometimes it doesn't do anything, and sometimes it quits the whole process (very annoying.) How can I make it so that hitting Ctrl-C always always works? It seems as if the times that it doesn't work are those where it is executing code in some other module.

Thanks much.

回答1:

You are probably running a background thread that is swallowing the signal, there is no real way of prohibiting that, compare http://www.dabeaz.com/python/GIL.pdf