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

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. Thanks in advance, Xian Chen

On 5/22/08, Xian Chen <hoganxian@gmail.com> wrote:
I do something like that, but I could not do it with a single deferred. I think the only way is to use a deferred per message. Then callback the appropriate deferred when its message arrives. That's all I can suggest given the limited information. Cameron

Xian Chen wrote:
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.

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@puzzling.org> wrote:

On Thu, May 22, 2008 at 11:20 PM, Xian Chen <hoganxian@gmail.com> wrote:
Here's an idiom that's served me well in the past: instead of using lots of 'if...else', use getattr(self, 'handle_' + messageName)(message). Replace 'handle_' with something appropriate for your message system. Decorators are also getting common as a way to register handlers for particular types of messages. -- Christopher Armstrong International Man of Twistery http://radix.twistedmatrix.com/ http://twistedmatrix.com/ http://canonical.com/

On 5/22/08, Xian Chen <hoganxian@gmail.com> wrote:
I do something like that, but I could not do it with a single deferred. I think the only way is to use a deferred per message. Then callback the appropriate deferred when its message arrives. That's all I can suggest given the limited information. Cameron

Xian Chen wrote:
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.

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@puzzling.org> wrote:

On Thu, May 22, 2008 at 11:20 PM, Xian Chen <hoganxian@gmail.com> wrote:
Here's an idiom that's served me well in the past: instead of using lots of 'if...else', use getattr(self, 'handle_' + messageName)(message). Replace 'handle_' with something appropriate for your message system. Decorators are also getting common as a way to register handlers for particular types of messages. -- Christopher Armstrong International Man of Twistery http://radix.twistedmatrix.com/ http://twistedmatrix.com/ http://canonical.com/
participants (4)
-
Andrew Bennetts
-
Cameron Dale
-
Christopher Armstrong
-
Xian Chen