[Twisted-Python] how to stop a service

Manlio Perillo manlio_perillo at libero.it
Sun Jun 11 04:43:38 EDT 2006

Manlio Perillo ha scritto:
> Jean-Paul Calderone ha scritto:
>> [...]
>>> Just another question: when running application with twistd, how can I
>>> return error codes from my application to operating system?
>>> That is: where (and how) can I safely call os.exit?
>> There's no support for this yet.
> What about storing the return code in the application instance and call
> os.exit(application.retcode) in an after shutdown system event?

Maybe a better place is inside the reactor:

In twisted.internet.base

# IReactorCore

    def stop(self, exitStatus=0):
        """See twisted.internet.interfaces.IReactorCore.stop.
        if not self.running:
            raise RuntimeError, "can't stop reactor that isn't running"
        self.exitStatus = exitStatus

    def crash(self):
        """See twisted.internet.interfaces.IReactorCore.crash.
        self.exitStatus = ???
        self.running = 0

    def sigInt(self, *args):
        """Handle a SIGINT interrupt.
        log.msg("Received SIGINT, shutting down.")
        self.callFromThread(self.stop, SIGINT)

    def sigBreak(self, *args):
        """Handle a SIGBREAK interrupt.
        log.msg("Received SIGBREAK, shutting down.")
        self.callFromThread(self.stop, SIGBREAK)

    def sigTerm(self, *args):
        """Handle a SIGTERM interrupt.
        log.msg("Received SIGTERM, shutting down.")
        self.callFromThread(self.stop, SIGTERM)

In twisted.scripts.twistd:

def runApp(config):

   log.msg("Server Shut Down.")

   from twisted.internet import reactor

Regards  Manlio Perillo

More information about the Twisted-Python mailing list