[Twisted-Python] Understanding Deferreds/callback further

Phil Mayers p.mayers at imperial.ac.uk
Fri Nov 3 11:12:25 EST 2006

Yi Qiang wrote:
> This is the part where I stumble on though, the exact implementation of 
> how to make it so that one calls back the other. 

One what calls back the other what?

You'll have issues with error handling and dropping workers if you're 
not careful to chain the deferreds correctly. I would try something like 
this personally.

class Worker:
     def gotJob(self, j):
         self.jobID = j.ID
         # do whatever
         callInThread(DoesntHoldGIL, j).addCallbacks(
             self.doneJob, self.failedJob
         ).addCallback(self.noJob, self.noJob)

     def doneJob(self, result):
         pb.callRemote('jobDone', self.jobID, result)

     def failedJob(self, f):
         pb.callRemote('jobFailed', self.jobID, f.getErrorMessage())

     def noJob(self, v):
         self.free = True

workers = Pool(Worker)

def checkForJobs():
     for worker in workers:
         if worker.free:
             worker.free = False
             job = pb.callRemote('getJob')
             job.addCallbacks(worker.gotJob, worker.noJob)

tsk = task.LoopingCall(checkForJobs)
# run every 10 seconds, start now
tsk.start(10, True)

More information about the Twisted-Python mailing list