[Twisted-Python] Understanding Deferreds/callback further

Phil Mayers p.mayers at imperial.ac.uk
Thu Nov 9 07:00:35 EST 2006


Yi Qiang wrote:
> On 11/8/06, *Phil Mayers* <p.mayers at imperial.ac.uk 
> <mailto:p.mayers at imperial.ac.uk>> wrote:
> 
> 
>         The "Monitor" portion of the clients seem superfluous - what
>         does it
>         achieve that having the worker submit the completion does not?
> 
> 
> Also, another reason why I wanted a monitor is because then I only have 
> to establish on connection to the PB server, vs having each worker 
> establish it's own connection, or passing around the remoteobj. 

(there's no need to CC me, I'm on the list)

Ok fine, you want a (superfluous IMHO) instance of a monitor class.

Do this:

class Worker:
   def issueJob(self, job):
     # return a deferred
     return reactor.callInThread(nonGILnonBlocking, job)

class Monitor:
   def connect(self):
     factory = pb.PB.ClientFactory()
     reactor.connectTCP(host, port, factory)
     factory.login(creds).addCallbacks(
       self.connected, self.connectfailed
     )

   def connected(self, remoteobj):
     self.remoteobj = remoteobj
     self.workerpool = SomeWorkerPool()
     for worker in self.workerpool:
       self.startWorker(worker)

   def startWorker(worker):
     self.remoteobj.callRemote('getJob').addCallback(
       self.startWorker2, worker
       # startWorker2 returns a deferred - the callback chain will be
       # paused here when that happens and continued when *that* deferred
       # finishes, with the callback value
     ).addCallback(
       self.workerDone,
       self.workerFailed,
       (worker,)
       {},
       (worker,)
       {},
     )

   def startWorker2(self, job, worker):
     deferred = worker.issueJob(job)
     return deferred

   def workerDone(self, result, worker):
     self.remoteobj.callRemote('jobDone', result)
     self.startWorker(worker)

   def workerFailed(self, failure, worker):
     self.remoteobj.callRemote('jobFailed', failure)
     self.startWorker(worker)

I *still* think you need a job ID number to key successes and failures 
at the server side.




More information about the Twisted-Python mailing list