[Twisted-Python] PB/Remote Object/Deferred question

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Wed Sep 9 12:56:39 EDT 2009


On 04:39 pm, cyclops at speakeasy.net wrote:
>I'm trying to understand the Perspective Broker, reading over the
>on-site documentation, specifically the article:
>
>http://twistedmatrix.com/projects/core/documentation/howto/pb- 
>usage.html
>
>and there is a section about the callRemote() function that I really
>don't understand :)
>
>First, consider these three lines:
>
>d = factory.getRootObject()
>d.addCallback(lambda object: object.callRemote("echo", "hello 
>network"))
>d.addCallback(lambda echo: 'server echoed: '+echo)
>
>Second, the documentation says, "...object.callRemote() returns a
>Deferred. Assuming the remote method was run without causing an
>exception (including an attempt to invoke an unknown method), the
>callback attached to that Deferred will be invoked with any objects
>that were returned by the remote method call."
>
>If I read this correctly, callRemote() returns a whole new Deferred
>object, which is *not* the same as the 'd' Deferred object we already
>have. So that *new* Deferred should be the one that the next callback
>(line 3) is added to, since the next callback is based on the success
>of the callRemote(). So wouldn't these be the correct lines:
>
>d = factory.getRootObject()
>d2 = d.addCallback(lambda object: object.callRemote("echo", "hello 
>network"))
>d2.addCallback(lambda echo: 'server echoed: '+echo)

When one Deferred is returned from a callback on another Deferred, the 
two Deferreds are "chained".  This means that the result of the one 
Deferred becomes the result of the other Deferred, and callbacks added 
to the other Deferred are not run until this happens.

Additionally, Deferred.addCallback returns self, so your version of the 
code is really exactly the same as the original version.

Jean-Paul



More information about the Twisted-Python mailing list