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()
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__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.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.