Search code examples
twisteddeferredaddcallback

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


I was attempting to trigger twisted memcache calls from getattribute and return values to my objects. Is this possible ? My thinking was that gatherResults waits for the call to succeed or fail and then returns the results - which it does but the interpreter returns a deferred to whatever is accessing the attribute.

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)

Solution

  • Just a heads to anyone who comes across this. This approach doesn't, can't, won't, should never, and will never work. Twisted will not return a value to your blocking code. So if you run into such a problem you need to rethink your approach. Twisted rocks in so many ways - just not this one.