On 05:06 pm, step...@thorne.id.au wrote:
On Fri, Jun 24, 2011 at 18:23,  <exar...@twistedmatrix.com> wrote:
Using the global reactor makes code less easily testable. �This isn't
specific to plugins, you should avoid the global reactor in all your
Twisted-using code. �Accept it as a parameter instead.

Ah, I did not do this successfully in my rewrite. I'd be interested to
see an example of the correct technique to use. Is this what you mean?

from twisted.internet import reactor
class SetupClass(service.Service):
   def doStart(self, reactor=reactor):
        reactor.callLater(3, self.done)

That's possible (and I'll assume you mean startService, not doStart), but the style I prefer is to keep the reactor as instance state, and initialize it in __init__.

   class SetupClass(service.Service):
       def __init__(self, reactor):
           self._reactor = reactor

       def startService(self):
           self._startupCall = self._reactor.callLater(3, self.done)

Some people like to make the reactor optional:

   class SetupClass(service.Service):
       def __init__(self, reactor=None):
           if reactor is None:
               from twisted.internet import reactor
           self._reactor = reactor

       def startService(self):
           self._startupCall = self._reactor.callLater(3, self.done)

Jean-Paul

_______________________________________________
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

Reply via email to