[Twisted-Python] help using deferred

Jean-Paul Calderone exarkun at divmod.com
Mon May 11 14:01:21 EDT 2009

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,
>        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
>                        d = self.d.addCallback(self.startVM,(new_vm,
>                        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.


More information about the Twisted-Python mailing list