[Twisted-Python] Plugin Example - review appreciated
exarkun at twistedmatrix.com
exarkun at twistedmatrix.com
Fri Jun 24 11:29:49 MDT 2011
On 05:06 pm, stephen at thorne.id.au wrote:
>On Fri, Jun 24, 2011 at 18:23, <exarkun at 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
More information about the Twisted-Python
mailing list