python:闭包和类
我需要注册一个 atexit
函数以与一个类一起使用(请参阅下面的 Foo
示例),不幸的是,我没有通过方法调用直接清理的方法:我无法控制的其他代码会调用 Foo.start()
和 Foo.end()
但有时不会调用 Foo。 end()
如果遇到错误,那么我需要自己清理。
在这种情况下,我可以对闭包使用一些建议:
class Foo:
def cleanup(self):
# do something here
def start(self):
def do_cleanup():
self.cleanup()
atexit.register(do_cleanup)
def end(self):
# cleanup is no longer necessary... how do we unregister?
闭包是否可以正常工作,例如在
do_cleanup
中,自绑定的值是否正确?如何取消注册 atexit() 例程?
有更好的方法吗?
编辑:这是Python 2.6.5
I need to register an atexit
function for use with a class (see Foo
below for an example) that, unfortunately, I have no direct way of cleaning up via a method call: other code, that I don't have control over, calls Foo.start()
and Foo.end()
but sometimes doesn't call Foo.end()
if it encounters an error, so I need to clean up myself.
I could use some advice on closures in this context:
class Foo:
def cleanup(self):
# do something here
def start(self):
def do_cleanup():
self.cleanup()
atexit.register(do_cleanup)
def end(self):
# cleanup is no longer necessary... how do we unregister?
Will the closure work properly, e.g. in
do_cleanup
, is the value of self bound correctly?How can I unregister an atexit() routine?
Is there a better way to do this?
edit: this is Python 2.6.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将注册表设置为全局注册表和调用其中函数的函数,并在必要时从其中删除它们。
Make a registry a global registry and a function that calls a function in it, and remove them from there when necessary.
我认为代码很好。无法取消注册,但您可以设置一个布尔标志来禁用清理:
最后,请记住,如果“当检测到 Python 致命内部错误或调用 os._exit() 时,程序会被 Python 未处理的信号杀死。”
I think the code is fine. There's no way to unregister, but you can set a boolean flag that would disable cleanup:
Lastly, bear in mind that
atexit
handlers don't get called if "the program is killed by a signal not handled by Python, when a Python fatal internal error is detected, or when os._exit() is called."self
在 do_cleanup 的回调中正确绑定,但事实上,如果您所做的只是调用该方法,您还可以直接使用绑定的方法。您使用atexit.unregister()来删除回调,但这里有一个问题,因为您必须取消注册您注册的同一函数,并且由于您使用了嵌套函数,这意味着您必须存储引用到该功能。如果您按照我的建议使用绑定方法,那么您仍然需要保存对它的引用:
请注意,您的代码仍然有可能在不调用
atexit
注册函数的情况下退出,例如,如果在 Windows 上使用 ctrl+break 中止进程,在 Linux 上使用 SIGABRT 终止进程。另外,另一个答案表明您可以只使用 __del__ 但这可能会在程序退出时进行清理出现问题,因为直到它需要访问的其他全局变量被删除后才可能调用它。
编辑注意到,当我写这个答案时,问题没有指定 Python 2.x。哦,好吧,无论如何我都会把答案留在这里,以防它对其他人有帮助。
self
is bound correctly inside the callback to do_cleanup, but in fact if all you are doing is calling the method you might as well use the bound method directly.You use
atexit.unregister()
to remove the callback, but there is a catch here as you must unregister the same function that you registered and since you used a nested function that means you have to store a reference to that function. If you follow my suggestion of using a bound method then you still have to save a reference to it:Note that it is still possible for your code to exit without calling ther
atexit
registered functions, for example if the process is aborted with ctrl+break on windows or killed with SIGABRT on linux.Also as another answer suggests you could just use
__del__
but that can be problematic for cleanup while a program is exiting as it may not be called until after other globals it needs to access have been deleted.Edited to note that when I wrote this answer the question didn't specify Python 2.x. Oh well, I'll leave the answer here anyway in case it helps anyone else.
由于 shanked 删除了他的帖子,我将再次支持
__del__
:这支持以下情况:
引用会消失
请注意,清理处理程序持有弱引用非常重要
到该对象,否则它会使该对象保持活动状态。
编辑:涉及 Foo 的循环不会被垃圾回收,因为 Foo 实现了
__del__
。为了允许在垃圾收集时删除循环,必须将清理工作从循环中取出。重要的是 Cleanup 对象没有对 Foo 的引用。
Since shanked deleted his posting, I'll speak in favor of
__del__
again:This supports the following cases:
reference goes away
Notice that it is important that the cleanup handler holds a weak reference
to the object, as it would otherwise keep the object alive.
Edit: Cycles involving Foo will not be garbage-collected, since Foo implements
__del__
. To allow for the cycle being deleted at garbage collection time, the cleanup must be taken out of the cycle.It's important that the Cleanup object has no references back to Foo.
你为什么不尝试一下呢?我只花了一分钟就检查过了。
(答案:是的)
但是,你可以简化它。不需要关闭。
并且为了不清理两次,只需在清理之前检查是否需要清理即可。
Why don't you try it? It only took me a minute to check.
(Answer: Yes)
However, you can simplify it. The closure isn't needed.
And to not cleanup twice, just check in the cleanup method if a cleanup is needed or not before you clean up.