[Twisted-Python] help using deferred

Luigi Conte luigiandcosolutions at gmail.com
Fri May 15 10:58:11 EDT 2009


2009/5/11 Jean-Paul Calderone <exarkun at divmod.com>

> On Mon, 11 May 2009 19:49:19 +0200, Luigi Conte <
> luigiandcosolutions at gmail.com> wrote:
> >
> > [snip]
> >in my script I have to use connect method and then I have to do some
> >operations first of calling the start method
> >1) start connection
> >
> >    def startConnection(self):
> >        d = my_api.connect(self.ctrl_ip, self.ctrl_port, self.user,
> >self.pwd)
> >        d.addCallback(self.postConnection)
>
> Since you omitted the definition of postConnection, I have no way to know
> what this does.
>
> >        d.addErrback(twisted.python.log.err)
>
> Regardless, this is the wrong place to insert this errback.  You should
> read
> the Deferred documentation to learn what consequence it will have on your
> program's flow.
>
> http://twistedmatrix.com/projects/core/documentation/howto/defer.html is
> probably a good place to start.
>
> >        print "Connection added"
> >        return d
> >2) operation before starting a virtual machine:
> >def newVMCfg(self, new_vms_cfg):
> >       #...
> >       #some operations
> >       #if condition valid I try to start the virtual machine
> >                        # is this the correct way to pass args to the
> start
> >method?
> >                        d = self.d.addCallback(self.startVM,(new_vm,
> >self.lnms[i])
> >                        print "started vm %s"%new_vm
> >        return d
> >
> >in the main process I call them as:
> >d = startConnection()
> >d.addCallback(newVMCfg, arg)
> >
> >Is it correct? Because the process stops at the first method called: I see
> >only "connection added".
>
> That should indicate to you that something is going wrong after that print
> statement is reached.  So examine what your program tries to do after that
> point.  You may want to use a debugger.  You may want to try writing some
> unit tests.
>
> Jean-Paul
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>

I looked at the documentation and I understood something about defferd and
callbacks. I edited my code but it does nothing...
I'm going to explain the scenario and what I want to do:
- there is an api that provides some methods (connect, start, disconnect).
- all those methods return a deferred object
- I have an object that first tries to connect, then reads a file and calls
the start method many times in a loop cicle of his method.

I want to use the connect and wait until I'm really connected. How can I do
this using the deferred object that the api returns to me?
I want to do the same thing each time I call the start method from the api:
I want to wait the completing of start operation before doing another start
invocation.

so I do:
in the main process:
    d = gest.connettiCLI_to_CTRL()

in gest class I have:
    def connettiCLI_to_CTRL(self):
        d = api.connect(self.ctrl_ip, self.ctrl_port, self.user, self.pwd)
        d.addCallback(self.cbConnected)
        return d

    def cbConnected(self, connectResult):
        return self.postConnection()

    def postConnection(self):
        # I do something but I don't call api's methods here
        # then I go to the next step returning the next method
        return self.interpretaNewVMCfg(self.vms_cfg)

    def interpretaNewVMCfg(self, new_vms_cfg):
        for i in candidati:
            #start operation in loop
            #here I want to control each start operation
            d = api.startVM(new_vm, self.lnms[i])
        return d

thank you so much
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20090515/1a48cb4f/attachment.htm 


More information about the Twisted-Python mailing list