Twisted Python: reactor and protocol
Question: I have several questions about the Twisted reactor.

1/ What kind of errors would crash/stop/terminate the reactor? what kind of
errors would not?

2/ I have two reactors, each running a different protocol. I have protocols A
and B.

One reactor creates a new instance of protocol A every time it handles an
input. If there is an error in an instance of protocol A, that error only
affects the instance, and the reactor keeps running smoothly.

The other reactor runs only ONE instance of protocol B (specifically I use it
to handle standard IO). It is simply like this:

    
    
    stdio.StandardIO(ProtocolB())
    reactor.run()
    

If an input somehow gives an error in Protocol B, then it affects the entire
reactor. Does the reactor actually stop in that case?

3/ In the case of the second reactor above, is it possible to create a new
instance of protocol B to replace the old instance if an error is detected?


Answer: > 1/ What kind of errors would crash/stop/terminate the reactor? what kind of
> errors would not?

A rule of thumb is: `reactor` runs until `reactor.stop()` is called by you or
in response to an expected event e.g., SIGINT signal (keyboard interrupt).

> If an input somehow gives an error in Protocol B, then it affects the entire
> reactor. Does the reactor actually stop in that case?

No, exceptions in your code won't stop the reactor:

    
    
    import sys
    from twisted.internet import reactor, task
    
    def raise_exception():
        raise RuntimeError
    reactor.callWhenRunning(raise_exception)
    
    task.LoopingCall(sys.stderr.write, '.').start(.4) # heartbeat
    reactor.callLater(5, reactor.stop) # stop reactor
    reactor.run()
    

> 2/ I have two reactors, each running a different protocol. I have protocols
> A and B.

There should be only one reactor whatever number of protocols is.

> 3/ In the case of the second reactor above, is it possible to create a new
> instance of protocol B to replace the old instance if an error is detected?

You can but you shouldn't do it. if `connectionMade`, `lineReceived` raise
exceptions it is a bug and you should fix it.

Here's an example that relaunches after exception. It is just a demonstration
that it is possible, _don't use it_ in a actual code.

    
    
    from twisted.internet import reactor
    from twisted.internet.stdio import StandardIO
    from twisted.protocols.basic import LineReceiver
    
    prompt = ">>>> "
    class ReverseLineProtocol(LineReceiver):
        delimiter = '\n'
    
        def connectionMade(self):
            self.sendLine("Write everything in reverse.")
            self.transport.write(prompt)
    
        def lineReceived(self, line):
            if line == 'raise':
                reactor.callLater(1, launch)
                raise RuntimeError
            self.sendLine(line[::-1])
            self.transport.write(prompt)
    
    def launch():
        StandardIO(ReverseLineProtocol())
    
    launch()
    reactor.run()
    

