[Twisted-Python] Deferred.callback() v. Deferred.called

Lenny G Arbage alengarbage at yahoo.com
Wed Feb 8 13:23:30 EST 2006

I think I'm seeing a difference between when a
deferred's callback is invoked and when the .called
member variable is set, and was hoping someone might
be able to explain.

(And let me apologize in advance if what I'm trying to
do is hairbrained or in some way abusive to the

First, a little context.  I have some code that I use
to test with, and it follows the standard deferred
chaining pattern, i.e.,

def alldone(res):

def trial3(res, ...):

def trial2(res, ...):

def trial1(...):
    d = someFunc1(...)

def maintest():

...and I wanted to try putting all the trial
invocations down in maintest, but still wait for the
result of a preceeding test before invoking the next,
so I made up a little function that does, basically,

def waitForDeferredResult(d):
    while not d.called:
    return d.result

...and then,

def maintest():
    d1 = trial1()
    res1 = waitForDeferredResult(d1)
    d2 = trial2(res1)
    res2 = waitForDeferredResult(d2)

Perhaps this is hairbrained, as the first strategy
works perfectly, but it sure would make it easier to
comment out one or two (or all but one or two) tests
when I just want to focus on hacking on that

However, the while loop in waitForDeferredResult ends
(.called == True) before the callback is actually
called in the case where the the Deferred in the trial
is also chained.  For example, if someFunc1() is set
up something like this:

    d = someFunc1-1()
    return d

    d = threads.deferToThread(someThing)
    return d

    d = threads.deferToThread(someOtherThing)
    return d

...then it appears that .called becomes true after the
deferred in someFunc1-1() returns, but before
someFunc1-2() is invoked.  If, on the other hand, I
chain from one trialX() to the next (as in the top
example) the callback isn't invoked until after
someFunc1-2() returns.  Anyone know why?


Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 

More information about the Twisted-Python mailing list