
Hi, I am working on the VIFF project (viff.dk) which uses Twisted. I found out that our code is sometimes inefficient because we are generating many deferreds (maybe about 10000) in a callback. While doing that, no network communication is performed. Therefore, I investigated the possibility of adding a function to the reactor which is called every iteration and from which the iteration could be called safely. Then, we could generate all deferreds in that function and activate the reactor from to time. See the attached patch for details. Some of our code runs twice as fast when using that hack. Are there any chances something similar might be included in Twisted? Or does anyone have a better solution for the described problem? Furthermore, I have a question, which is probably related. The documentation of IReactorCore says about the iterate() function: "The reactor must have been started (via the run() method) prior to any invocations of this method. It must also be stopped manually after the last call to this method (via the stop() method). This method is not re-entrant: you must not call it recursively; in particular, you must not call it while the reactor is running." This looks to me as if the reactor needs to be running and not running at the same time so that iterate() can be called. Is there an error in my reasoning? Best regards, Marcel Keller --- /usr/lib/python2.5/site-packages/twisted/internet/base.py 2008-07-29 22:13:54.000000000 +0200 +++ internet/base.py 2009-02-20 12:27:42.000000000 +0100 @@ -1127,17 +1127,32 @@ self.startRunning(installSignalHandlers=installSignalHandlers) self.mainLoop() + + def setLoopCall(self, f, *args, **kw): + self.loopCall = (f, args, kw) + + + def myIteration(self, t): + # Advance simulation time in delayed event + # processors. + self.runUntilCurrent() + + if (t is None): + t2 = self.timeout() + t = self.running and t2 + + self.doIteration(t) + + if ("loopCall" in dir(self)): + f, args, kw = self.loopCall + f(*args, **kw) + def mainLoop(self): while self._started: try: while self._started: - # Advance simulation time in delayed event - # processors. - self.runUntilCurrent() - t2 = self.timeout() - t = self.running and t2 - self.doIteration(t) + self.myIteration(None) except: log.msg("Unexpected error in main loop.") log.err()