[Twisted-Python] a little thingo

Timothy Stebbing tim.stebbing at nunatak.com.au
Sat Sep 11 07:44:33 EDT 2004


gday,

LordVan had a query the other night, he was writing a test app for a PB
server, and on receiving his root object from a deferred, he wanted to
call each consecutive remote method to test them.

Initially he was adding each test as a callback of the last, and passing
on the root object each time. He wanted to know how to run many callbacks
on the initial return value consecutivly, without chaining.

I wrote this little snippet, which in the end he didn't use, however I
thought it might be of interest to others for some purpose.

problems I didnt bother fixing:

a)  if a test callback modifies the value, the next test gets a modified
value. if this is an issue the Repeater should copy the value.

b) you can't pass extra args to the callbacks.

from twisted.internet import reactor, defer

class Repeater(object):

    def dFactory(self, v):
        while 1:
            d = defer.Deferred()
            d.callback(v)
            yield d

    def __call__(self, v, list):
        g = self.dFactory(v)
        l = []
        for i in list:
            l.append(g.next().addCallback(i))
        return l

##TEST:
def test1(v):
    print v

def test2(v):
    print v

def test3(v):
    print v

r = Repeater()
d = defer.Deferred()
d.callback('foo')
print d.addCallback(r, [test1, test2, test3])
reactor.run()

-tjs




More information about the Twisted-Python mailing list