Tag: 键盘中断

在没有尝试的情况下在Python中捕获键盘中断

有没有在Python中捕获KeyboardInterrupt事件的一些方法,而不是把所有的代码放在try – except语句中? 如果用户按下ctrl – c,我想干净地退出。

键盘中断与python的多处理池

我怎样才能处理与python的多处理池的KeyboardInterrupt事件? 这是一个简单的例子: 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__ […]