在程序退出之前做一些事情
如何拥有一个在程序退出之前执行的函数或其他东西?我有一个脚本将在后台不断运行,我需要它在退出之前将一些数据保存到文件中。有这样做的标准方法吗?
How can you have a function or something that will be executed before your program quits? I have a script that will be constantly running in the background, and I need it to save some data to a file before it exits. Is there a standard way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
查看
atexit
模块:http://docs.python.org/ library/atexit.html
例如,如果我想在应用程序终止时打印一条消息:
请注意,这对于脚本的正常终止非常有效,但在所有情况下都不会调用它(例如致命的内部错误)。
Check out the
atexit
module:http://docs.python.org/library/atexit.html
For example, if I wanted to print a message when my application was terminating:
Just be aware that this works great for normal termination of the script, but it won't get called in all cases (e.g. fatal internal errors).
如果您希望某些内容始终运行,即使出现错误,请像这样使用
try:finally:
-如果您还想处理异常,您可以在
之前插入
except:
代码>最后:If you want something to always run, even on errors, use
try: finally:
like this -If you want to also handle exceptions you can insert an
except:
before thefinally:
如果您通过引发
KeyboardInterrupt
(例如按Ctrl-C)来停止脚本,您可以将其作为标准异常捕获。您也可以用同样的方式捕获SystemExit
。我提到这一点只是为了让您了解这一点;执行此操作的“正确”方法是上面提到的
atexit
模块。If you stop the script by raising a
KeyboardInterrupt
(e.g. by pressing Ctrl-C), you can catch that just as a standard exception. You can also catchSystemExit
in the same way.I mention this just so that you know about it; the 'right' way to do this is the
atexit
module mentioned above.这是根据其他答案改编的版本。
它应该可以正常退出、终止和 PyCharm 停止按钮(我可以确认的最后一个)(尚未完全测试)。
This is a version adapted from other answers.
It should work (not fully tested) with graceful exits, kills, and PyCharm stop button (the last one I can confirm).
这可以处理正常退出以及使用
kill
或Ctrl+C
终止进程:This can handle normal exit as well as killing the process with
kill
orCtrl+C
:如果您有在程序的整个生命周期中都存在的类对象,您还可以使用 __del__(self) 方法从类中执行命令:
这也适用于正常程序结束,如果执行被中止,肯定会有一些例外:
If you have class objects, that exists during the whole lifetime of the program, you can also execute commands from the classes with the
__del__(self)
method:this works on normal program end as well if the execution is aborted, for sure there will be some exceptions:
您还可以使用上下文管理器执行类似于 @Brian C. Lane 的回答的操作:
You could also use a context manager to do something similar to @Brian C. Lane's answer :