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

Lenny G Arbage alengarbage at yahoo.com
Wed Feb 8 11:23:30 MST 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
methodology...)

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):
    pass

def trial3(res, ...):
    ...
    d.addCallback(alldone)
    ...

def trial2(res, ...):
    ...
    d.addCallback(trial3)
    ...

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

def maintest():
    trial1(...)


...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:
        reactor.iteratoe(0.1)
    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
functionality.

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:

someFunc1():
    d = someFunc1-1()
    return d

someFunc1-1():
    d = threads.deferToThread(someThing)
    d.addCallback(someFunc1-2)
    return d

someFunc1-2():
    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?

Thanks,
Lenny

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 




More information about the Twisted-Python mailing list