keyboardinterrupt

Catching KeyboardInterrupt in Python during program shutdown

老子叫甜甜 提交于 2019-11-26 18:51:39
问题 I'm writing a command line utility in Python which, since it is production code, ought to be able to shut down cleanly without dumping a bunch of stuff (error codes, stack traces, etc.) to the screen. This means I need to catch keyboard interrupts. I've tried using both a try catch block like: if __name__ == '__main__': try: main() except KeyboardInterrupt: print 'Interrupted' sys.exit(0) and catching the signal itself (as in this post): import signal import sys def sigint_handler(signal,

Ctrl-C crashes Python after importing scipy.stats

非 Y 不嫁゛ 提交于 2019-11-26 16:12:34
问题 I'm running 64-bit Python 2.7.3 on Win7 64-bit. I can reliably crash the Python interpreter by doing this: >>> from scipy import stats >>> import time >>> time.sleep(3) and pressing Control-C during the sleep. A KeyboardInterrupt is not raised; the interpreter crashes. The following is printed: forrtl: error (200): program aborting due to control-C event Image PC Routine Line Source libifcoremd.dll 00000000045031F8 Unknown Unknown Unknown libifcoremd.dll 00000000044FC789 Unknown Unknown

Capture keyboardinterrupt in Python without try-except

萝らか妹 提交于 2019-11-26 03:06:22
问题 Is there some way in Python to capture KeyboardInterrupt event without putting all the code inside a try - except statement? I want to cleanly exit without trace if user presses Ctrl + C . 回答1: Yes, you can install an interrupt handler using the module signal, and wait forever using a threading.Event: import signal import sys import time import threading def signal_handler(signal, frame): print('You pressed Ctrl+C!') sys.exit(0) signal.signal(signal.SIGINT, signal_handler) print('Press Ctrl+C

Keyboard Interrupts with python's multiprocessing Pool

与世无争的帅哥 提交于 2019-11-26 00:46:38
问题 How can I handle KeyboardInterrupt events with python\'s multiprocessing Pools? Here is a simple example: from multiprocessing import Pool from time import sleep from sys import exit def slowly_square(i): sleep(1) return i*i def go(): pool = Pool(8) try: results = pool.map(slowly_square, range(40)) except KeyboardInterrupt: # **** THIS PART NEVER EXECUTES. **** pool.terminate() print \"You cancelled the program!\" sys.exit(1) print \"\\nFinally, here are the results: \", results if __name__ =