title: What is the preferred way to pass the reactor to a protocol in Twisted?
body: <p>In the <a href="http://twistedmatrix.com/documents/12.2.0/api/twisted.internet.reactor.html" rel="nofollow">twisted docs</a>, it says:</p>
  
  <blockquote>
    <p>New application code should prefer to pass and accept the reactor as a parameter where it is needed, rather than relying on being able to import this module to get a reference. This simplifies unit testing and may make it easier to one day support multiple reactors (as a performance enhancement), though this is not currently possible.</p>
  </blockquote>
  
  <p>What is the preferred way of doing this? In particular, what is the best way to pass it to a protocol? (Especially when the standard twisted.internet.protocol.Factory is all that's needed.)</p>
  
  <p><em>Edit</em>: To clarify, since the protocol is not instantiated directly, I am wondering if the method below is the preferred way, or if there is a better way of allowing access to the reactor in the protocol.</p>
  
  <pre><code>class MyFactory(protocol.Factory):
      def __init__(self, rct):
          self.reactor = rct
  
  class MyProtocol(protocol.Protocol):
      def __init__(self):
          self.reactor = None
  
      def connectionMade(self):
          self.reactor = self.factory.reactor
  
  if __name__=="__main__":
      from twisted.internet import reactor
      f = MyFactory(reactor)
      f.protocol = MyProtocol
      reactor.listenXXX(whatever)
      reactor.run()
  </code></pre>
  
  <p>It seems a little bit silly to make a factory class solely for the purpose of accessing the reactor, since wanting access to the reactor must be fairly common. </p>