[Twisted-Python] Understanding Deferreds/callback further

Yi Qiang yqiang at gmail.com
Thu Nov 2 18:33:47 EST 2006


Hi guys,
I am still having trouble writing programs that will make the most use of
deferreds.  I will describe the problem I am trying to solve and the
solution I've tried.

The problem:
I am trying to write a client that gets a job from a PB server, processes
it, submits the result and gets another job.  I need to be able to do this
for multiple jobs (i.e. I need to be able to get 5 jobs, and start 5 similar
chains: get job --> work on job --> submit result --> get another job).
This looks like a perfectly solvable problem using deferreds since working
on the job might take a while and using callbacks seems to be the most
appropriate.

So I have something like this in my main method:

monitor = Monitor(server, port)
monitor.connect()


    def connect(self):
        factory = pb.PBClientFactory()
        reactor.connectTCP(self.hostname, self.port, factory)
        return factory.login(self.credentials).addCallback(
                            self._connected).addErrback(
                            self._catch_failure)

Then in self._connected I would like to start the chain of getting
jobs/processing them/submitting jobs:

    def _connected(self, remoteobj):
        self.remoteobj = remoteobj

        for worker in self.workers:
            self.get_jobs(worker).addCallback(
                                    self._got_job, worker).addCallback(
                                    self.get_jobs, worker).addErrback(
                                    self._catch_failure)

_got_job takes both a job and a worker and starts to process the job.  Which
for now returns a defer.succeed().
This is the part where I stumble on though, the exact implementation of how
to make it so that one calls back the other.
Any help would be appreciated.  Please let me know also if you need more
specific information.

Thanks,
Yi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20061102/6855a763/attachment.htm 


More information about the Twisted-Python mailing list