[Twisted-Python] reset/restart reactor problem

Jean-Paul Calderone exarkun at divmod.com
Sat Jun 24 21:47:47 MDT 2006


On Sun, 25 Jun 2006 04:19:22 +0100, Joseph Newman <jfn20 at cam.ac.uk> wrote:
>Hi All,
>
>I am porting an academic middleware project, currently implemented in  C++ 
>and Java, to python. It extensively uses multicast for  communicating 
>general events and TCP to communicate with a database.  The multicast 
>messages that I send out depend on the results that I  first obtain from the 
>database. I would prefer to serialise each of  these requests in turn, but 
>its not immediately obvious to me what  the best way to do this using 
>Twisted is.

There are quite a few possible techniques to apply here.  One I would
recommend is to keep a list of objects which represent pending tasks
which need to be executed serially.  When each task completes, pop the
next one from the list and begin processing it.  When a new task needs
to be performed, push it onto the list.

>
>If I could reset the reactor after each client/server transaction  that 
>would be by far the easiest.

Twisted does not currently support restartable reactors.  This may be
addressed someday, but I don't think this is actually the best solution
to the problem you are facing.

>I have been experimenting with 
>threadedselectreactor, but I currently don't have an event loop as  such 
>that is independent of the reactor event loop. Nearly all the  examples deal 
>with processes that either act as servers or clients,  rather than 
>combinations of the two.

TSR probably isn't helpful here.  I would suggest something like this:

  class JobList:
      def __init__(self):
          self.jobs = []

      def add(self, job):
          self.jobs.append(job)
          if len(self.jobs) == 1:
              self._doJob()

      def _doJob(self):
          job = self.jobs[0]
          d = job.do()
          d.addErrback(log.err)
          d.addCallback(self._finishedJob)

      def _finishedJob(self, result):
          self.jobs.pop(0)
          if self.jobs:
              self._doJob()

Hope this helps,

Jean-Paul




More information about the Twisted-Python mailing list