[Twisted-Python] help using deferred

Luigi Conte luigiandcosolutions at gmail.com
Mon May 11 06:24:54 EDT 2009

Hi people,
I'm luigi and I'm a student at Politecnico di Milano, Italy. I'm working for
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 defferred
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 hoping
someone could help me in how to add the method in the deferred and how to
know when it is completely invoked.
# Connect
def connect(self, ctrl_host, ctrl_port, user, passwd):
        """Connect to an Usher control server.

        @param ctrl_host: fqdn where Usher controller is running
        @type ctrl_host: string
        @param ctrl_port: port number Usher controller is listening on
        @type ctrl_port: int
        @param user: username of user connecting to Usher controller
        @type user: string
        @param passwd: password of user connecting to Usher controller
        @type passwd: string

        return: Deferred which returns and L{UsherResult}
        rtype: twisted.internet.defer.Deferred
        # serialize access to connect
        if not self.connect_lock.acquire(False):
            raise UsherEx, "Connection Pending"

        if self.factory:

        # reset vms
        self.vms = {}
        # reset passwd to None until we're connected
        self.passwd = None
        self.factory = APIFactory()
        self.factory.api = self
        # connect to server
        reactor.connectSSL(ctrl_host, ctrl_port, self.factory,
        d = self.factory.login(credentials.UsernamePassword(user, passwd),
        d.addErrback(self._handle_err, misc.whoami())
        d.addCallback(self._connected_to_ctrl, ctrl_host, ctrl_port, user,
        return d

# start
def start(self,
        """Start a VM or set of VMs

        @param cluster: Name of cluster to which VMs should belong
        @type cluster: string
        @param prefix: prefix to prepend to VM names
        @type prefix: string
        @param vmlist: list of strings of VMs to start
        @type vmlist: list of strings
        @param count: Number of consecutive VMs to start
        @type count: int
        @param first: Lowest number of VM to start
        @type first: int
        @param last: Highest number of VM to start
        @type last: int
        @param dregex: regular expression to match against destination LNM
        @type dregex: list of strings
        @param dlist: list of strings to match against destination LNM names
        @type dlist: list of strings
        @param dryrun: Show what would have happened if the command had been
                run without the dryrun command (at the instant that the
                command was run of course)
        @type dryrun: bool
        @param eid: mechanism for admin user to be able to specify an
                username for forming suffix for regex
        @type eid: string
        @param kw: Extra arguments passed to controller for use by plugins
            LNM.  Any keywords starting with 'vm_' are added to VM's uargs
            parameter.  Note, the controller actually checks for two kw
            arguments: ram and ip_addrs.  Though these are, strictly
            VM parameters, they're considered important enough that the
            controller checks for them.
        @type kw: dict

        @return: Deferred which returns an L{UsherResult}
        @rtype: twisted.internet.defer.Deferred

        @raise UsherNotConnected: Raised when not connected to Usher
        @raise UsherPBDeadRefEx: Raised when capability for Usher
            Controller goes stale.  Must reconnect when this happens
        @raise UsherInvalidInputEx: Raised when an invalid input or
            combination of parameters is received.

        @attention: The following conditions must hold:
                1. first, last, and count must be non-negative
                2. specifying first requires last or count
                3. cannot use count with vmlist
                4. cannot use count with both first and last

        # make sure we're connected
        if not self.aref:
            raise UsherNotConnectedEx

        # combine these to reduce params
        if ((first and first < 0) or (last and last < 0) or (count and count
            raise UsherInvalidInputEx(
                    "first, last, and count must be non-negative")
        vmrange = (first,last,count)
        # arg check
        self._arg_check(vmlist, vmrange)

        # get final destination list
        dlist = self._namecomplete(dlist, suffix=self.suffix)
        dregex = self._regex_append(dregex, dlist)
        dlist = self._regex_filter(dregex, self.lnms.keys())

        # complete names (if necessary) in the vmlist
        vmlist = self._namecomplete(vmlist, cluster, eid)
        # check that the vms specified don't already exist
        self._check_vmlist(vmlist, isin=True)
        vmlist = self._get_vm_names_to_start(vmlist, cluster, prefix,

        # controller handles eid from kw, so add it
        if eid:
            kw['eid'] = eid

        if dryrun:
            d = UsherResult('dryrun', msg = 'started:' + linesep +
            return d

            if vmlist:
                d = self.aref.callRemote("start", vmlist, dlist,
                d.addErrback(self._handle_err, misc.whoami())
                raise UsherInvalidInputEx(
                        "No VMs started.  No unique VMs specified in
            return d
        except pb.DeadReferenceError, ex:
            raise UsherPBDeadRefEx

If you want, all the package is at
in the client folder you'll find the api.py

Thank you very much hoping someone will help this totally inexpert user,
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20090511/4f45a018/attachment.htm 

More information about the Twisted-Python mailing list