简体   繁体   中英

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM