title: Connecting twice with twisted - how to do that correctly?
body: <p>I want to use twisted (and StarPy which is a protocol implementation for asterisk ami) to connect to an asterisk server. The application initiates a outgoing fax there. I found some hints on my problem, but I cannot find out how to handle this correctly.</p>
  
  <p>The first fax is sent out correctly.</p>
  
  <blockquote>
    <p>Problem is, if I call twisted for the
    second time, the application keeps
    hanging in main loop.</p>
  </blockquote>
  
  <p>I know I may NOT do this like here:</p>
  
  <pre><code>from starpy import manager
  from twisted.internet import reactor
  
  def main():
      f = manager.AMIFactory(cUser, cPass)
      print "Login"
      df = f.login(cServer, cPort)
  
      def onLogin(protocol):
          print "Logoff again"
          df = protocol.logoff()
  
          def onLogoff( result ):
              print "Logoff erfolgt"
              reactor.stop()
  
          return df.addCallbacks( onLogoff, onLogoff )
  
      def onFailure( reason ):
          print "Login failed"
          print reason.getTraceback()
  
      df.addCallbacks( onLogin, onFailure )
      return df
  
  if __name__ == "__main__":
      reactor.callWhenRunning( main )
      reactor.run(installSignalHandlers=0)
      print "runned the first time"
  
      reactor.callWhenRunning( main )
      reactor.run(installSignalHandlers=0)
      print "will never reach this point"
  </code></pre>
  
  <p>I simplified the code - it does nothing than login + logoff again. It will never return from the second reactor.run() call.</p>
  
  <p>How is this done correctly? I'm stuck here - thanks in advance.</p>
  
  <p>Best Regards,
  Florian.</p>