[Twisted-Python] wired problem with deferreds

markus espenhain me at mocisoft.com
Sun Feb 3 12:41:47 MST 2008


hi

im stuck with the following deferred construct - the problem is that cb2
is called with None instead of the expected argument

from twisted.internet import defer

cd = None

def req1():
    return defer.Deferred().addCallback(req2)

def req2(a):
    print 'req2', a
    global cd
    if not cd:
        cd = defer.Deferred().addCallback(req3)
    return cd

def req3(a):
    print 'req3', a
    return a

def cb1(a):
    print 'cb1', a
    return a

def cb2(a):
    print 'cb2', a
    return a


d1 = req1().addCallback(cb1)
d1.callback('X')
d2 = req1().addCallback(cb2)
d2.callback('X')
cd.callback('S')

output:

req2 X
req2 X
req3 S
cb1 S
cb2 None # <- ?

req1 fetches a mapping for given arguments - req2 tries to create an
object + additional resquests by the result of req1 - but these objects
should only be created once - so far i didn't find an acceptable
workaround for that - req3 applys some modifications but the problem
occurs also without the step in req3 

is there something i overlooked so that cb2 isn't called with the
required arg?

thanks
markus





More information about the Twisted-Python mailing list