[Twisted-Python] help using deferred
luigiandcosolutions at gmail.com
Mon May 11 10:55:40 EDT 2009
I'm sure you're saying only good things but I'm so inexpert in twisted that
I can't understand very well what to do.
I'll try to explain all that I want to do and how I did it wrongly:
first I have to call the connect method from the api and it returns a
deferred so I do:
in conn_to_ctrl i have:
d = api.connect(...)
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?
I do so:
in examinecfg i have:
# tha same d that i used to add api.connect!
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.
In the main I have:
defer = conn_to_ctrl()
or have I to add to defer also the examinecfg?
thank you very much
2009/5/11 Jean-Paul Calderone <exarkun at divmod.com>
> On Mon, 11 May 2009 12:24:54 +0200, Luigi Conte <
> luigiandcosolutions at gmail.com> wrote:
> >Hi people,
> >I'm luigi and I'm a student at Politecnico di Milano, Italy. I'm working
> >a graduate thesis on a framework called usher. It provides an API to do
> >connect e command operations. It manages all those calls with a defferred
> >object (i think). In fact when I call the connect it returns me a
> >object. All that i want to know is how to call the other operations with
> >this object and how to know when an operation is completely done.
> >The procedure I call is simple: connect method, start method many times,
> >stop method, disconnect method. I want to call these methods sequentially
> >because I have to fallow a list of commands so I have to be sure the
> >previous methods are already been invoked. I'll post the main methods
> >someone could help me in how to add the method in the deferred and how to
> >know when it is completely invoked.
> The Deferred tells you two things. It tells you when an operation has
> completed and it tells you the result of the operation. It tells you
> *when* by calling your callback function at that time. It tells you
> what *result* by the value it passes to that callback function.
> So if you want to connect, then do the "start" operation many times, then
> stop, then disconnect, you have some Deferreds and a simple recursion-
> or loop-like structure. For example,
> # First, set up the connection.
> taskDeferred = connect(...)
> def cbConnected(connectResult):
> # This function will run once the Deferred returned by `connect´
> # fires. Here, "start" something.
> return start(...)
> def cbStarted(startResult):
> # Now the Deferred returned by `start´ has fired.
> if someStopCondition:
> # If we decide it is time to stop, then stop.
> return stop(...)
> # Otherwise, run `start´ again
> startAgainDeferred = start(...)
> # And call this callback again with this new call's result.
> return startAgainDeferred
> def cbStopped(stopResult):
> # The only exit from the earlier "loop" is through the code path
> # which calls `stop´. This callback is called when the Deferred
> # returned by that call fires. Do the disconnect now.
> return disconnect(...)
> def cbDisconnected(disconnectResult):
> print "All done"
> # It's possible something will go wrong in the above. To make sure any
> # exceptions are reported, add a logging errback at the very end to
> # display whatever might go wrong.
> Hope this helps,
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Twisted-Python