2

如果在函数中定义了一个类实例,当函数退出时,该实例会因为超出范围而被自动销毁。这可以通过一个小程序简单验证:

class A(object):
    def __del__(self):
        print 'deleting A ', id(self)

class B(A):
    def __init__(self):
        self.a = A()

    def __del__(self):
        print 'deleting B ', id(self)
        super(B, self).__del__()


def test():
    b = B()
    print 'this is ', b

test()

输出是:

this is  <__main__.B object at 0x01BC6150>
deleting B  29122896
deleting A  29122896
deleting A  29122960

但我遇到了一个奇怪的问题。当我从 novaclient 继承一个类时,该实例永远不会被自动销毁。

from novaclient.v1_1.client import Client as NovaClient

class ViviNovaClient(NovaClient):
    def __init__(self, auth_token, url, tenant_id):
        super(ViviNovaClient, self).__init__(None, None, tenant_id, auth_url = 'http')
        self.client.management_url = url
        self.client.auth_token = auth_token
        self.client.used_keyring = True;
        LOG.info('creating <ViviNovaClient> %d' % id(self))

    def __del__(self):
        LOG.info('deleting <ViviNovaClient> %d' % id(self))


if __name__ == '__main__':
    def test():
        client = ViviNovaClient('53ef4c407fed45de915681a2d6aef1ee',                                   
          'http://135.251.237.130:8774/v2/082d8fd857f44031858827d149065d9f',
           '082d8fd857f44031858827d149065d9f')

    test()

输出是:

2013-05-24 23:08:03 32240 INFO vivi.vivimain.ViviNovaClient [-] creating <ViviNovaClient> 26684304

在这个测试中,'client' 对象没有被破坏。所以我想知道什么会阻止“客户端”对象自动销毁?

4

1 回答 1

5

因为很多其他对象也存储了对客户端的引用,形成了很多引用循环

例如,从源代码

class Client(object):
    def __init__(self, username, **etc):
        password = api_key
        self.project_id = project_id
        self.flavors = flavors.FlavorManager(self)
        # etc.

我们看到 Client 会保存一个 FlavorManager,FlavorManager 的初始化器(它是一个Manager)也将保存对输入 Client 的引用:

class Manager(utils.HookableMixin):
    def __init__(self, api):
        self.api = api

在 Python 启动循环收集器之前,这些对象不会立即被删除。请注意,添加__del__方法会阻止循环收集器运行

于 2013-05-24T07:44:32.740 回答