[Twisted-Python] passing data in callbacks or errbacks of deferreds

Andrew Bennetts andrew-twisted at puzzling.org
Fri Dec 5 22:43:05 EST 2003


On Fri, Dec 05, 2003 at 10:28:56PM -0500, rahul at reno.cis.upenn.edu wrote:
> Hi Folks,
> I have the following need. My program acts as a xmlrpc client using
> the Proxy class, peeking at the top of a stack to find its payload. It gets
> the payload by peeking and then sends it out on the network. What I want
> to do, if the send was successful, is to pop the stack, and if not successful,
> to resend the payload. Since callback and errback only tahe the succesful
> response(message) or failure instance respectively as an argument, how do
> I do this, as I am not able to access the stack inside these functions,
> (I cant use a module global, as each program has multiple stack instances,
> and i need to pass a particular one).
> 
> Am i missing something?

I think what you are looking for is:

     d.addCallback(cb, extraArg1, extraArg2)  # etc

which will be called as

     cb(result, extraArg1, extraArg2)

E.g.:

>>> from twisted.internet.defer import Deferred
>>> d = Deferred()
>>> def cb(result, foo):
...     print result * 2, foo
... 
>>> d.addCallback(cb, 'Hey!')
<Deferred at 0x401e5a6c>
>>> d.callback(5)
10 Hey!


[Glancing at http://twistedmatrix.com/documents/howto/defer, it does mention
 this in passing, but it doesn't give any examples or make it clear.  If you
 read the section on "Basic Callback Functions"
 (http://twistedmatrix.com/documents/howto/defer#auto9) carefully, and you
 know what you're looking for, it does describe this feature.]

-Andrew.





More information about the Twisted-Python mailing list