垃圾收集完成后的方法调用

Method call after garbage collection is finished

我有一个 class“Foo”,其中对象注册在一个名为 FooManager 的对象中。离开函数范围后,只有当我知道所有“标记为删除”的 Foos 时,才能删除 Foo 对象。

class FooManager:
    
    def __init__(self):
        self.deletion_list = []
    
    def delete_tagged_foos(self):
        #Deletion process
        pass

class Foo:

    def __init__(self, FooManager):
        self.FooManager = FooManager

    def __del__(self):
        self.FooManager.deletion_list.append(self)

        if garbage_collection_finished():
            self.FooManager.delete_tagged_foos()


def foo_processing(manager):
    foo1 = Foo(manager)
    foo2 = Foo(manager)
    foo3 = Foo(manager)
    
    # Tag foo2 and foo3 for deletion (but not foo1)
    # and perform deletion process once every foo is tagged
    return foo1


manager = FooManager()

foo1 = foo_processing(manager)

如何获取布尔值 garbage_collection_finished()?

当函数 returns:

为什么不自己调用方法
foo1 = foo_processing(manager)
manager.delete_tagged_foos()

在调用delete_tagged_foos()时,foo2foo3都已经在管理器中重新注册了,所以不会再被删除。

如何跟踪 FooManager 中的 foo 实例并将其用作创建临时 foo 实例的函数的装饰器:

import sys

class FooManager:
    
    def __init__(self):
        self.foos = []

    def registerFoo(self,foo):
        foo.FooManager = self
        self.foos.append(foo)
        print("+1")

    def cleanFoos(self,func):
        def callFunc(*args,**kwargs):
            result = func(*args,**kwargs)
            # 3 refs = self.foos, foo in comprehension, getrefcount param
            orphanFoos = [foo for foo in self.foos if sys.getrefcount(foo)<=3]
            # process orphans as needed
            # leave them in self.foos if they are not to be deleted
            self.foos  = [foo for foo in self.foos if foo not in orphanFoos]
            # ...
            print("foo count:",len(self.foos))
            return result 
        return callFunc
        
class Foo:

    def __init__(self, FooManager):
        FooManager.registerFoo(self)

    def __del__(self): print("deleted")

用法:

manager = FooManager()

@manager.cleanFoos
def foo_processing(manager):
    foo1 = Foo(manager)
    foo2 = Foo(manager)
    foo3 = Foo(manager)
    
    # Tag foo2 and foo3 for deletion (but not foo1)
    # and perform deletion process once every foo is tagged
    return foo1

foo1 = foo_processing(manager)

输出:

+1
+1
+1
foo count: 1
deleted
deleted