Python捕获全局的KeyboardInterrupt异常的方法实现
作者:Looooking
KeyboardInterrupt异常是Python中的一个标准异常,它通常发生在用户通过键盘中断了一个正在运行的程序,本文主要介绍了Python捕获全局的KeyboardInterrupt异常的方法实现,感兴趣的可以了解一下
当然,像下面这种情况。
你要是把所有代码像下面那样都放到 try, except 的情况,就当我什么也没说。
import time def main(): print('before ...') time.sleep(10) print('after ...') if __name__ == '__main__': try: main() except KeyboardInterrupt: print('\nKeyboardInterrupt ...') print('the end')
root@master ~/w/python3_learning# python3 test.py before ... ^C KeyboardInterrupt ... the end
一般情况下,程序运行过程当中要执行的代码量会比较大,一般用户执行 Ctrl + C 程序就报错 KeyboardInterrupt 停止了。
import time def main(): print('before ...') time.sleep(10) print('after ...') if __name__ == '__main__': main() print('the end')
root@master ~/w/python3_learning# python3 test.py before ... ^CTraceback (most recent call last): File "test.py", line 11, in <module> main() File "test.py", line 6, in main time.sleep(10) KeyboardInterrupt
但是有时候,我们希望用户在 Ctrl + C 之后再继续执行一些清理操作。
import sys import time def suppress_keyboard_interrupt_message(): old_excepthook = sys.excepthook def new_hook(exctype, value, traceback): if exctype != KeyboardInterrupt: old_excepthook(exctype, value, traceback) else: print('\nKeyboardInterrupt ...') print('do something after Interrupt ...') sys.excepthook = new_hook def main(): print('before ...') time.sleep(10) print('after ...') if __name__ == '__main__': suppress_keyboard_interrupt_message() main() print('the end')
root@master ~/w/python3_learning# python3 test.py before ... ^C KeyboardInterrupt ... do something after Interrupt ...
由于 suppress_keyboard_interrupt_message 函数中的 new_hook 是自定义的,所以你也不一定局限于只处理某种异常,甚至对所有异常做统一处理也是可以的。
到此这篇关于Python捕获全局的KeyboardInterrupt异常的方法实现的文章就介绍到这了,更多相关Python KeyboardInterrupt异常内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!