As you said, what I really want is a dispatcher which can dispatch different msgs to corresponding callbacks.<br><br>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.<br>
<br>I got the same idea with Cameron, maybe it is not very efficient way to define one defer object for one msg.<br><br>So, I come here to ask whether you guys has a better way or not.<br><br>Thanks,<br>Xian<br><br><div class="gmail_quote">
On Fri, May 23, 2008 at 10:26 AM, Andrew Bennetts <<a href="mailto:andrew-twisted@puzzling.org">andrew-twisted@puzzling.org</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class="Wj3C7c">Xian Chen wrote:<br>
> Hi All,<br>
><br>
> This is a fundamental question about Twisted.<br>
><br>
> In the normal case, all the callbacks belonging to one defer object will be<br>
> triggered following the chain sequence. ( Let's assume no errback will be<br>
> fired.)<br>
><br>
> such as callback1--->callbac2--->callbackN.<br>
><br>
> However, if I want to trigger callback1 when MSG1 is ready, callback2 when MSG2<br>
> is ready, and so on, how can I do that?<br>
><br>
> Of course, the MSGs are coming at random sequence.<br>
<br>
</div></div>It sounds like you don't want a callback chain at all. Do you really want<br>
all callbacks from 3 to N run when MSG3 arrives? That sounds strange.<br>
<br>
I'm guessing you want a simple dispatch function as the callback on the<br>
Deferred. i.e., something roughly like this:<br>
<br>
def dispatchMessage(msg):<br>
handler = messageHandlersDict[<a href="http://msg.id" target="_blank">msg.id</a>]<br>
return handler.run(msg)<br>
<br>
d.addCallback(dispatchMessage)<br>
<br>
How messageHandlersDict is populated depends on exactly what you're doing...<br>
<br>
It's not clear from your email whether "MSG1", "MSG2" etc refer to different<br>
message types, or messages tagged with a session/request identifier, or<br>
something else. So if my advice doesn't make sense to you, maybe reply with a<br>
bit more detail about what you're doing, and what you want to happen?<br>
<font color="#888888"><br>
-Andrew.<br>
</font><div><div></div><div class="Wj3C7c"><br>
<br>
_______________________________________________<br>
Twisted-Python mailing list<br>
<a href="mailto:Twisted-Python@twistedmatrix.com">Twisted-Python@twistedmatrix.com</a><br>
<a href="http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python" target="_blank">http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python</a><br>
</div></div></blockquote></div><br>