This has been driving me crazy for a while -- for some reason
reactor.stop() in the _error errback in example below raises
error.ReactorNotRunning. In order to stop the reactor, I have to do
reactor.callWhenRunning(reactor.stop) (or I did reactor.callLater(0,
...) until I discovered callWhenRunning).
In the example, I bind to a low port to make sure error is triggered.
#!/usr/local/bin/python3
from twisted.internet import reactor, defer, protocol
from twisted.internet.protocol import Factory
from twisted.internet.endpoints import TCP4ServerEndpoint
def _port(port):
print('got', port)
def _error(err):
print('got err', err)
print('is reactor running?', reactor.running)
print('is reactor running?', (lambda: reactor.running)())
reactor.stop()
# reactor.callWhenRunning(reactor.stop)
d = TCP4ServerEndpoint(reactor, 123).listen(Factory())
d.addCallback(_port)
d.addErrback(_error)
d.addErrback(print)
reactor.run()
OTOH, in the following example reactor.stop() is stoppig the reactor
properly:
#!/usr/local/bin/python3
from twisted.internet import reactor, defer
def cb(res):
print('running?', reactor.running)
if res == 'bar':
raise Exception()
reactor.stop()
def eb(err):
print('running?', reactor.running)
print(err)
reactor.stop()
d = defer.Deferred()
d.addCallback(cb)
d.addErrback(eb)
#reactor.callWhenRunning(d.callback, 'foo')
reactor.callWhenRunning(d.callback, 'bar')
reactor.run()
Any ideas?