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

Ted Toth txtoth at gmail.com
Tue Oct 25 15:57:18 EDT 2011

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?

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):
    def __init__(self, d):
        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))

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(
            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(

    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()

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: {}

More information about the Twisted-Python mailing list