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

rahul at reno.cis.upenn.edu rahul at reno.cis.upenn.edu
Fri Dec 5 23:36:04 EST 2003

Thanks! For some reason I missed it completely in the defer document. Someone
run me over with a large truck!

The program I am writing is a general astronomy data analysis pipeline system,
with a workflow similar to BPELWS, but way simpler, and multiprotocol.
Twisted just makes that very easy. The exact reason for http based xmlrpc
is that some of the peers on this pipeline have very poor networking
(our telescopes being at 9500 ft in Taiwan's central mountains..), and thats
why I need to be able to back off on messages and persist messages if necessary.

My current program mixes twisted and threads, mostly because I havent grokked
twisted zen yet. But I intend to, ofcourse, and the next task will
be protocolozing stack access so that new additions to the stack can be treated
as events. 

Thanks for helping me along..and also big thanks to the author of "Twisted From Scratch"
On Sat, Dec 06, 2003 at 02:43:05PM +1100, Andrew Bennetts wrote:
> 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.
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

More information about the Twisted-Python mailing list