问题描述
import time
try:
time.sleep(10)
finally:
print "clean up"
clean up
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
KeyboardInterrupt
如果需要捕获其他操作系统级别的中断,请查看信号模块:
http://docs.python.org/library/signal.html
信号示例
from signal import *
import sys, time
def clean(*args):
print "clean me"
sys.exit(0)
for sig in (SIGABRT, SIGBREAK, SIGILL, SIGINT, SIGSEGV, SIGTERM):
signal(sig, clean)
time.sleep(10)
解决方法
有什么方法可以在运行的Python脚本被其他脚本,键盘中断等终止之前停止运行最后一条命令。