在属性访问“__getattribute__”上使用扭曲代理内存缓存调用

Using twisted to proxy memcache calls on attribute access '__getattribute__'

我试图触发从 getattribute 和 return 值到我的对象的扭曲内存缓存调用。这可能吗 ?我的想法是,gatherResults 等待调用成功或失败,然后 returns 结果 - 它确实这样做了,但解释器 returns 被推迟到访问属性的任何东西。

def __getattribute__(self, key):
        exempt = ['__providedBy__', '__class__', '__provides__', '__dict__', 'uid']
        if key in exempt:
            return object.__getattribute__(self, key)
        else:
            print key
            addr = IPv4Address('TCP', '127.0.0.1', 11211)
            mc_pool = MemCachePool(addr, maxClients=10)
            uid = object.__getattribute__(self, key)

            def return_res(res):
                return res

            deferred_calls = [mc_pool.get(key)]
            d = defer.gatherResults(deferred_calls, consumeErrors = True)
            d.addCallback(return_res)

请注意遇到此问题的任何人。这种方法不会、不能、不会、不应该、也永远不会奏效。 Twisted 不会 return 为您的阻塞代码赋值。因此,如果您 运行 遇到这样的问题,您需要重新考虑您的方法。扭曲的岩石在很多方面 - 只是不是这个。