def react(main, argv=(), _reactor=None):
 if (_reactor is None):
  from twisted.internet import reactor as _reactor
 finished = main(_reactor, *argv)
 codes = [0]
 stopping = []
 _reactor.addSystemEventTrigger('before', 'shutdown', stopping.append, True)
 def stop(result, stopReactor):
  if stopReactor:
   try:
    _reactor.stop()
   except ReactorNotRunning:
    pass
  if isinstance(result, Failure):
   if (result.check(SystemExit) is not None):
    code = result.value.code
   else:
    log.err(result, 'main function encountered error')
    code = 1
   codes[0] = code
 def cbFinish(result):
  if stopping:
   stop(result, False)
  else:
   _reactor.callWhenRunning(stop, result, True)
 finished.addBoth(cbFinish)
 _reactor.run()
 sys.exit(codes[0])

def react ( main , argv = ( ) , _reactor = None ) : if ( _reactor is None ) : from twisted . internet import reactor as _reactor finished = main ( _reactor , * argv ) codes = [ 0 ] stopping = [ ] _reactor . addSystemEventTrigger ( 'before' , 'shutdown' , stopping . append , True ) def stop ( result , stopReactor ) : if stopReactor : try : _reactor . stop ( ) except ReactorNotRunning : pass if isinstance ( result , Failure ) : if ( result . check ( SystemExit ) is not None ) : code = result . value . code else : log . err ( result , 'main function encountered error' ) code = 1 codes [ 0 ] = code def cbFinish ( result ) : if stopping : stop ( result , False ) else : _reactor . callWhenRunning ( stop , result , True ) finished . addBoth ( cbFinish ) _reactor . run ( ) sys . exit ( codes [ 0 ] )
call c{main} and run the reactor until the l{deferred} it returns fires .