[英]do something when terminate a python program
I want make a python script do something such as publishing a message before terminating it. 我想让python脚本执行某些操作,例如在终止消息之前发布消息。 I have tried
signal
and it works when I input Ctrl+c
in cmd window. 我已经尝试过
signal
并且在cmd窗口中输入Ctrl+c
时它可以工作。 However, it seems inoperative when I just directly close cmd window or kill the process from Windows task manager. 但是,当我直接关闭cmd窗口或从Windows任务管理器中终止进程时,它似乎不起作用。 So how can I reach my goal in the condition of above situation?
那么在上述情况下如何达到我的目标呢? Thanks for all suggestions!
感谢所有建议!
Take a look at the atexit module: 看一下atexit模块:
http://docs.python.org/library/atexit.html http://docs.python.org/library/atexit.html
Example: 例:
import atexit
def exit_handler():
print('My program just quit')
atexit.register(exit_handler)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.