I&#39;m sure you&#39;re saying only good things but I&#39;m so inexpert in twisted that I can&#39;t understand very well what to do.<br>I&#39;ll try to explain all that I want to do and how I did it wrongly:<br>first I have to call the connect method from the api and it returns a deferred so I do:<br>
in conn_to_ctrl i have:<br>d = api.connect(...)<br>return d<br><br>now I do some operations with config files to call many times the start method from the api (I have to start many virtual machines) and the api.start returns a deferred so what have I to do now?<br>
I do so:<br>in examinecfg i have:<br># tha same d that i used to add api.connect!<br>self.d.addCallback(api.start, (method_args))<br>return d<br><br>Then I have to call the disconnect method from the api. But i tried to print out something to fallow the executing but I only see printing the first method that call the connection. after that the process ends.<br>
<br>In the main I have:<br>defer = conn_to_ctrl()<br>or have I to add to defer also the examinecfg?<br><br>thank you very much<br><br><div class="gmail_quote">2009/5/11 Jean-Paul Calderone <span dir="ltr">&lt;<a href="mailto:exarkun@divmod.com">exarkun@divmod.com</a>&gt;</span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="im">On Mon, 11 May 2009 12:24:54 +0200, Luigi Conte &lt;<a href="mailto:luigiandcosolutions@gmail.com">luigiandcosolutions@gmail.com</a>&gt; wrote:<br>

&gt;Hi people,<br>
&gt;I&#39;m luigi and I&#39;m a student at Politecnico di Milano, Italy. I&#39;m working for<br>
&gt;a graduate thesis on a framework called usher. It provides an API to do<br>
&gt;connect e command operations. It manages all those calls with a defferred<br>
&gt;object (i think). In fact when I call the connect it returns me a defferred<br>
&gt;object. All that i want to know is how to call the other operations with<br>
&gt;this object and how to know when an operation is completely done.<br>
&gt;The procedure I call is simple: connect method, start method many times,<br>
&gt;stop method, disconnect method. I want to call these methods sequentially<br>
&gt;because I have to fallow a list of commands so I have to be sure the<br>
&gt;previous methods are already been invoked. I&#39;ll post the main methods hoping<br>
&gt;someone could help me in how to add the method in the deferred and how to<br>
&gt;know when it is completely invoked.<br>
<br>
</div>The Deferred tells you two things.  It tells you when an operation has<br>
completed and it tells you the result of the operation.  It tells you<br>
*when* by calling your callback function at that time.  It tells you<br>
what *result* by the value it passes to that callback function.<br>
<br>
So if you want to connect, then do the &quot;start&quot; operation many times, then<br>
stop, then disconnect, you have some Deferreds and a simple recursion-<br>
or loop-like structure.  For example,<br>
<br>
    # First, set up the connection.<br>
    taskDeferred = connect(...)<br>
    def cbConnected(connectResult):<br>
        # This function will run once the Deferred returned by `connect´<br>
        # fires.  Here, &quot;start&quot; something.<br>
        return start(...)<br>
    taskDeferred.addCallback(cbConnected)<br>
<br>
    def cbStarted(startResult):<br>
        # Now the Deferred returned by `start´ has fired.<br>
        if someStopCondition:<br>
            # If we decide it is time to stop, then stop.<br>
            return stop(...)<br>
        # Otherwise, run `start´ again<br>
        startAgainDeferred = start(...)<br>
        # And call this callback again with this new call&#39;s result.<br>
        startAgainDeferred.addCallback(cbStarted)<br>
        return startAgainDeferred<br>
    taskDeferred.addCallback(cbStarted)<br>
<br>
    def cbStopped(stopResult):<br>
        # The only exit from the earlier &quot;loop&quot; is through the code path<br>
        # which calls `stop´.  This callback is called when the Deferred<br>
        # returned by that call fires.  Do the disconnect now.<br>
        return disconnect(...)<br>
    taskDeferred.addCallback(cbStopped)<br>
<br>
    def cbDisconnected(disconnectResult):<br>
        print &quot;All done&quot;<br>
    taskDeferred.addCallback(cbDisconnected)<br>
<br>
    # It&#39;s possible something will go wrong in the above.  To make sure any<br>
    # exceptions are reported, add a logging errback at the very end to<br>
    # display whatever might go wrong.<br>
    taskDeferred.addErrback(twisted.python.log.err)<br>
<br>
Hope this helps,<br>
<br>
Jean-Paul<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>
</blockquote></div><br>