[Twisted-Python] the sequence of executing defers' callbacks

Xian Chen hoganxian at gmail.com
Thu May 22 23:20:26 EDT 2008


As you said, what I really want is a dispatcher which can dispatch different
msgs to corresponding callbacks.

Of course, I can write a dispatchMessage(msg) function by using lots of "if
... else ", but I want a more elegant way. I hope defers can help me.

I got the same idea with Cameron, maybe it is not very efficient way to
define one defer object for one msg.

So, I come here to ask whether you guys has a better way or not.

Thanks,
Xian

On Fri, May 23, 2008 at 10:26 AM, Andrew Bennetts <
andrew-twisted at puzzling.org> wrote:

> Xian Chen wrote:
> > Hi All,
> >
> > This is a fundamental question about Twisted.
> >
> > In the normal case, all the callbacks belonging to one defer object will
> be
> > triggered following the chain sequence. ( Let's assume no errback will be
> > fired.)
> >
> > such as callback1--->callbac2--->callbackN.
> >
> > However, if I want to trigger callback1 when MSG1 is ready, callback2
> when MSG2
> > is ready, and so on, how can I do that?
> >
> > Of course, the MSGs are coming at random sequence.
>
> It sounds like you don't want a callback chain at all.  Do you really want
> all callbacks from 3 to N run when MSG3 arrives?  That sounds strange.
>
> I'm guessing you want a simple dispatch function as the callback on the
> Deferred.  i.e., something roughly like this:
>
>    def dispatchMessage(msg):
>        handler = messageHandlersDict[msg.id]
>        return handler.run(msg)
>
>    d.addCallback(dispatchMessage)
>
> How messageHandlersDict is populated depends on exactly what you're
> doing...
>
> It's not clear from your email whether "MSG1", "MSG2" etc refer to
> different
> message types, or messages tagged with a session/request identifier, or
> something else.  So if my advice doesn't make sense to you, maybe reply
> with a
> bit more detail about what you're doing, and what you want to happen?
>
> -Andrew.
>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20080523/2cbf480d/attachment.htm 


More information about the Twisted-Python mailing list