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 &quot;if ... else &quot;, 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 &lt;<a href="mailto:andrew-twisted@puzzling.org">andrew-twisted@puzzling.org</a>&gt; 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>
&gt; Hi All,<br>
&gt;<br>
&gt; This is a fundamental question about Twisted.<br>
&gt;<br>
&gt; In the normal case, all the callbacks belonging to one defer object will be<br>
&gt; triggered following the chain sequence. ( Let&#39;s assume no errback will be<br>
&gt; fired.)<br>
&gt;<br>
&gt; such as callback1---&gt;callbac2---&gt;callbackN.<br>
&gt;<br>
&gt; However, if I want to trigger callback1 when MSG1 is ready, callback2 when MSG2<br>
&gt; is ready, and so on, how can I do that?<br>
&gt;<br>
&gt; Of course, the MSGs are coming at random sequence.<br>
<br>
</div></div>It sounds like you don&#39;t want a callback chain at all. &nbsp;Do you really want<br>
all callbacks from 3 to N run when MSG3 arrives? &nbsp;That sounds strange.<br>
<br>
I&#39;m guessing you want a simple dispatch function as the callback on the<br>
Deferred. &nbsp;i.e., something roughly like this:<br>
<br>
 &nbsp; &nbsp;def dispatchMessage(msg):<br>
 &nbsp; &nbsp; &nbsp; &nbsp;handler = messageHandlersDict[<a href="http://msg.id" target="_blank">msg.id</a>]<br>
 &nbsp; &nbsp; &nbsp; &nbsp;return handler.run(msg)<br>
<br>
 &nbsp; &nbsp;d.addCallback(dispatchMessage)<br>
<br>
How messageHandlersDict is populated depends on exactly what you&#39;re doing...<br>
<br>
It&#39;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. &nbsp;So if my advice doesn&#39;t make sense to you, maybe reply with a<br>
bit more detail about what you&#39;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>