[Tutor] Object destruction

Dimitar Ivanov dimitarxivanov at gmail.com
Tue May 24 09:27:17 EDT 2022


Hi all,

I'm trying to come up with a (working) design of tracking an object
throughout its lifecycle but I'm unable to find quite what I'm looking for,
so I hope you folks will be able to give me some tips.

I have an object that is being created and I want to "trace" that object in
a separate static class that other threads and objects will be able to
access if necessary:

class TestClass:

    def __init__(self, name):
        self.name = name
        self._finalizer = weakref.finalize(self, self.finalize)

    def do_stuff(self):
        print(f"Object {self.name} doing stuff")
        Tracer.trace(self.name)
        time.sleep(5)

    def finalize(self):
        print(f"Entered finalize method for object {self.name}")
        Tracer.untrace(self.name)

Here, I have the static object that "traces" those objects:

class Tracer:

    traced_objects = []

    @staticmethod
    def trace(some_object):
        Tracer.traced_objects.append(some_object)

    @staticmethod
    def untrace(some_object):
        Tracer.traced_objects.remove(some_object)

And here's my main method where I test creating the objects, kicking off
their do_stuff methods in a thread and then deleting them and checking if
they've been removed from the traced_objects list in the Tracer class:

if __name__ == '__main__':
    objectOne = TestClass("Object1")
    objectTwo = TestClass("Object2")
    thrd1 = Thread(target=objectOne.do_stuff)
    thrd2 = Thread(target=objectTwo.do_stuff)
    thrd1.start()
    thrd2.start()
    thrd1.join()
    thrd2.join()
    del objectOne
    del objectTwo
    print("Threads are done, checking Tracer's dict")
    print(f"Tracer's dict: {Tracer.traced_objects}")

It seems like the Finalizer is only kicking off the objects' finalize
methods once the program exits, which, if I'm reading the documentation
right, is the correct behaviour; however, I need that finalize method
kicked off as soon as any reference to those objects is removed. Can I
achieve that in any way?

Thanks a lot in advance and please, do let me know if my explanation is
vague and/or unclear!

Regards,
Dimitar


More information about the Tutor mailing list