I have just started to learn twisted and wrote a small tcp server/client using the Tcp4endpoint-class. Everything works fine, except for one thing.
In order to detect the event that an unavailble port is given to the server as listen port, I have added an errback to the endpoint-deferer. This errback is triggered, however, I am unable to exit the application from the errback. Reactor.stop causes another failure saying that reactor is not running, while for example sys.exit triggers another error. The output from the two latter is only seen when I do ctrl+c and gc hits.
My question is, is there any way to make the application exit (cleanly) after a listenFailure has occured?
A minimal example would help make your question more clear. However, based on many years of Twisted experience, I have an educated guess. I think you wrote a program something like this:
You are on the right track. Unfortunately you have an ordering issue. The reason
reactor.stopfails withReactorNotRunningis that thelistenDeferred fails ”before” you callreactor.run. That is, it has already failed by the time you didd.addErrback(listenFailed), solistenFailedwas called right away.There are a number of solutions to this. One is to write a .tac file and use services:
This is run using
twistd, liketwistd -y thisfile.tacAnother option is to use the low-level feature that services are based on,
reactor.callWhenRunning: