[Twisted-Python] perspective broker remote call returning a dictionary

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Wed Oct 26 11:09:54 EDT 2011


On 25 Oct, 07:57 pm, txtoth at gmail.com wrote:
>I'm trying to return a dictionary with objects as values from a remote
>call but it's not working. Can anyone tell me what I'm doing wrong or
>point me at an example that works?
>
>dserver.py
>--------------------------------------------------------------------------------------------------------------------------------
>from twisted.spread import pb
>
>class Test():
>    def __init__(self, data):
>        self.data = data
>
>class CopyTest(Test, pb.Copyable):
>    def __init__(self, test):
>        Test.__init__(self, test.data)
>
>class CopyDict(dict, pb.Copyable):

Subclassing both dict and Copyable is probably a major part of the 
problem.  There is one set of rules for serializing a dict and another 
incompatible set for serializing a Copyable.  It seems that in your 
case, the Copyable behavior seems to be in use, so all the dict contents 
are ignored.

Jean-Paul
>    def __init__(self, d):
>        dict.__init__(self)
>        for key in d.keys():
>            self[key] = CopyTest(d[key])
>            print key, self[key].data
>        print "Dict: ", pb.jelly(self)
>        print self
>
>
>class dTest(pb.Root):
>    def __init__(self):
>        self.d = {}
>        self.d['A'] = Test("data for A")
>        self.d['D'] = Test("data for D")
>        self.d['C'] = Test("data for C")
>        self.d['B'] = Test("data for B")
>
>    def remote_getD(self):
>        from dserver import CopyDict
>        foo =  CopyDict(self.d)
>        return foo
>
>if __name__ == '__main__':
>    from twisted.internet import reactor
>    d = dTest()
>    reactor.listenTCP(8789, pb.PBServerFactory(d))
>    reactor.run()
>
>---------------------------------------------------------------------------------------------------------------
>dclient.py
>---------------------------------------------------------------------------------------------------------------
>from twisted.spread import pb
>from twisted.internet import reactor
>from dserver import Test, CopyTest, CopyDict
>import sys
>
>class ReceiverDict(dict, pb.RemoteCopy):
>     def setCopyableState(self, state):
>        print "ReceiverDict: ", state
>        self.__dict__ = state
>#    pass
>pb.setUnjellyableForClass(CopyDict, ReceiverDict)
>
>class ReceiverTest(pb.RemoteCopy, Test):
>     def setCopyableState(self, state):
>        print "ReceiverTest: ", state
>        self.__dict__ = state
>#   pass
>pb.setUnjellyableForClass(CopyTest, ReceiverTest)
>
>class Receiver(object):
>    def __init__(self):
>        self.domain = None
>
>    def runTests(self):
>        self.connect( ).addCallback(
>            lambda _: self.getD( )).addCallback(
>            self._catchFailure).addCallback(
>            lambda _: reactor.stop( ))
>
>    def connect(self):
>        factory = pb.PBClientFactory( )
>        reactor.connectTCP("localhost", 8789, factory)
>        return factory.getRootObject( ).addCallback(self._connected)
>
>    def _connected(self, rootObj):
>        self.domain = rootObj
>
>    def getD(self):
>        print "Getting object..."
>        return self.domain.callRemote(
>            'getD').addCallback(
>            self._gotD)
>
>    def _gotD(self, d):
>        print "Got :", d
>        for key in d.keys():
>            print "key: ",key
>            print d[key]
>        return d
>
>    def _catchFailure(self, failure):
>        print "Error:", failure #.getErrorMessage( )
>
>t = Receiver()
>t.runTests()
>reactor.run()
>
>-----------------------------------------------------------------------------------------------------------
>Debug output:
>
>[tedx at comms twisted]$ python dserver.py
>A data for A
>C data for C
>B data for B
>D data for D
>Dict:  ['dserver.CopyDict', ['dictionary']]
>{'A': <dserver.CopyTest instance at 0x1a0ccb0>, 'C': <dserver.CopyTest
>instance at 0x1a0ccf8>, 'B': <dserver.CopyTest instance at 0x1a0cd40>,
>'D': <dserver.CopyTest instance at 0x1a0cd88>}
>
>[tedx at comms twisted]$ python dclient.py
>Getting object...
>ReceiverDict:  {}
>Got : {}
>Error: {}
>
>_______________________________________________
>Twisted-Python mailing list
>Twisted-Python at twistedmatrix.com
>http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python



More information about the Twisted-Python mailing list