防止 __del__ 在多处理中被调用 [英] prevent __del__ from being called in multiprocessing

查看:47
本文介绍了防止 __del__ 在多处理中被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在玩 multiprocessing 时,我注意到在下面的脚本中,__del__ 被调用了两次(一次在子进程中,一次在父进程中).

When playing around with multiprocessing I noticed that in the following script, __del__ is called twice (once in the child processes and once in the parent).

class myclass(object):

    def __init__(self,val):
        self.val=val
        print ("Initializing %s"%str(self.val))

    def __del__(self):
        print ("deleting %s"%str(self.val))


if __name__ == "__main__":
    import multiprocessing
    p=multiprocessing.Pool(4)
    obj_list=p.map(myclass,range(30))
    raw_input()

对于这个脚本,没关系……但是如果 __del__ 有副作用怎么办?(我能想到的一个可能的例子是释放某种锁定文件).有没有办法防止 __del__ 被调用两次?

For this script, it doesn't matter ... but what if __del__ has side-effects? (One possible example that I can think of would be to release some sort of lock file). Is there a way to prevent __del__ from being called twice?

推荐答案

__del__ 通常用于终结一个对象,而不是外部资源,因此在两个进程中都调用它是有意义的(因为,当然,在 fork 之后,两者都有自己的对象副本).在给定的进程中尝试阻止任何 __del__ 的使用并不是一个好主意,但是在您确实,真的 需要它来关闭外部资源的地方,例如文件系统上的文件,它不会被 fork 复制,你的析构函数应该在这样做之前检查预期的资源是否真的需要清理.释放某种锁定文件"可能是也可能不是这种情况,具体取决于您的实施方式.

__del__ is normally used to finalize an object, not an external resource, so it makes sense to call it in both processes (since, of course, both have their own copies of the object after a fork). It's not a good idea to try to prevent any use of __del__ in a given process, but in places where you really, really need it to close out an external resource like a file on the filesystem, which doesn't get duplicated by a fork, your destructor should just check to see if the expected resource really needs cleaning up before doing so. That may or may not be the case for "releasing some kind of lock file", depending on how you're implementing that.

如果这是一个选项,您可能需要考虑使用with"上下文管理器进行资源获取和释放,而不是依赖于垃圾收集的变幻莫测.

If it's an option, you might want to look at doing resource acquisition and release using "with" context managers instead of depending on the vagaries of garbage collection.

这篇关于防止 __del__ 在多处理中被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆