[Twisted-Python] using a reactor in a loop

Tom Brown brown at esteem.com
Thu Mar 1 10:21:50 MST 2007


Hello,

I am trying to use a reactor in a loop. The application will go through the 
loop twice then hang when trying to stop the protocol. Here's my loop:

def main():
  factory = Factory()
  factory.numProtocols = 0
  factory.protocol = AqapProtocol
  while True:
    print 'running server'
    # do other work ...
    reactor.listenTCP(48555, factory)
    reactor.run() # stopped by protocol

Here's how the protocol stops the reactor:

  def connectionLost(self, reason):
    self.factory.numProtocols -= 1
    if not self.factory.numProtocols:
      print 'stopping reactor'
      reactor.stop()
      print 'reactor stopped'

I see the 'reactor stopped' get printed out twice through the loop but I don't 
see 'running server' get printed out a third time. So, reactor.run() is not 
returning. I must be missing something or going at this completely wrong. Any 
suggestions?

Thanks,
Tom




More information about the Twisted-Python mailing list