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

Andrew Bennetts andrew-twisted at puzzling.org
Thu May 22 20:26:36 MDT 2008

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)


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?


More information about the Twisted-Python mailing list