如何在被Python杀死之前运行最后一个函数?

如何在被Python杀死之前运行最后一个函数?,第1张

如何在被Python杀死之前运行最后一个函数?
import timetry:    time.sleep(10)finally:    print "clean up"clean upTraceback (most recent call last):  File "<stdin>", line 2, in <module>KeyboardInterrupt

如果需要捕获其他 *** 作系统级别的中断,请查看信号模块:

http://docs.python.org/library/signal.html

信号示例
from signal import *import sys, timedef clean(*args):    print "clean me"    sys.exit(0)for sig in (SIGABRT, SIGBREAK, SIGILL, SIGINT, SIGSEGV, SIGTERM):    signal(sig, clean)time.sleep(10)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5601807.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存