man kill:
Some of the more commonly used signals:
1 HUP (hang up)
2 INT (interrupt)
3 QUIT (quit)
6 ABRT (abort)
9 KILL (non-catchable, non-ignorable kill)
14 ALRM (alarm clock)
15 TERM (software termination signal)
import time
import signal
def CtrlCHandler(signum, frame):
current_time = time.time()
sys.exit(0)
signal.signal(signal.SIGINT, CtrlCHandler)
catch
KeyboardInterrupt測試:
我回答過的問題: Python-QA
那就捕信号吧,ctrl-c也是信号,
SIGINTCtrl-C或者kill -2 pid的时候都会到这个CtrlCHandler.建议用信号而不是
KeyboardInterruptCtrl+C 会产生KeyboardInterrupt异常,捕获它就可以了