[Twisted-Python] async code structure question

Mary Gardiner mary-twisted at puzzling.org
Thu Feb 10 14:37:42 EST 2005


On Thu, Feb 10, 2005, snacktime wrote:
> Specifically what I am having the most difficulty with is this.  In
> Do() is where the Deferred is created that needs to have it's
> callbacks fired.  How can I fire off that callback from authorize()
> and have authorize() pass it the final data?

Somehow authorize needs to have the deferred accessible. There's no
magical Deferred way of doing this, you do it the way that you'd make
any other piece of data accessible to a method: pass it to the method,
or having it accessible as self.d would be the standard ways.

So that means that Do() will change slightly

>     def Do():
>       d = defer.Deferred()
>       c = ProcessTransaction()
>       reactor.callLater(0, c.Do,data)
>       d.addCallback(self.PrintData)
>       d.addErrback(log.err)

        # finish here by storing d somewhere so that authorize can later
        # get access to it

And then authorize does something like this to fire off the callback
with data:

        # assume that the Deferred is accessible to authorize as self.d
        self.d.callback(data)

Once you use .callback, data will be passed to the first callback method
(self.PrintData) and so on.

The precise method by which you pass d around to the various classes is
as much a matter of individual program design as passing any other bit
of data around: config variables, strings, ints, etc etc.

-Mary




More information about the Twisted-Python mailing list