[Twisted-Python] redefine signal handlers
Hello everyone, I would like to run some cleanup code when my Twisted app receives a signal (SIGINT/SIGBREAK/SIGTERM). I saw that "_SignalReactorMixin" sets the handlers and that "ReactorBase" defines the default handlers : def sigInt(self, *args): """Handle a SIGINT interrupt. """ log.msg("Received SIGINT, shutting down.") self.callFromThread(self.stop) def sigBreak(self, *args): """Handle a SIGBREAK interrupt. """ log.msg("Received SIGBREAK, shutting down.") self.callFromThread(self.stop) def sigTerm(self, *args): """Handle a SIGTERM interrupt. """ log.msg("Received SIGTERM, shutting down.") self.callFromThread(self.stop) My question is how can I redefine them, other than monkey patching or inheriting the reactor and over-riding them (which I'd rather not do since some of my code uses the windows reactor when on windows since I was having problems with windows event)? Is there such a mechanism, something like setDefaultSig("SIGINT, mySigIntHandler)? Thank you, Gabriel
Hi Gabriel, On Wed, Feb 4, 2009 at 10:15 AM, Gabriel Rossetti <gabriel.rossetti@arimaz.com> wrote:
Hello everyone,
I would like to run some cleanup code when my Twisted app receives a signal (SIGINT/SIGBREAK/SIGTERM).
I saw that "_SignalReactorMixin" sets the handlers and that "ReactorBase" defines the default handlers : ... My question is how can I redefine them, other than monkey patching or inheriting the reactor and over-riding them (which I'd rather not do since some of my code uses the windows reactor when on windows since I was having problems with windows event)? Is there such a mechanism, something like setDefaultSig("SIGINT, mySigIntHandler)?
One easy way to do it is to reactor.run(installSignalHandlers=False) and then manually install your own signal handlers in the usual way (using the python signal library). Cheers, Reza -- Reza Lotun Senior Software Engineer Peer Technologies Limited reza@getpeer.com
On Wed, 4 Feb 2009 10:32:19 +0000, Reza Lotun <reza@getpeer.com> wrote:
Hi Gabriel,
On Wed, Feb 4, 2009 at 10:15 AM, Gabriel Rossetti <gabriel.rossetti@arimaz.com> wrote:
Hello everyone,
I would like to run some cleanup code when my Twisted app receives a signal (SIGINT/SIGBREAK/SIGTERM).
I saw that "_SignalReactorMixin" sets the handlers and that "ReactorBase" defines the default handlers : ... My question is how can I redefine them, other than monkey patching or inheriting the reactor and over-riding them (which I'd rather not do since some of my code uses the windows reactor when on windows since I was having problems with windows event)? Is there such a mechanism, something like setDefaultSig("SIGINT, mySigIntHandler)?
One easy way to do it is to reactor.run(installSignalHandlers=False) and then manually install your own signal handlers in the usual way (using the python signal library).
If you do this, you'll break spawnProcess. Fortunately, if you just install a SIGINT handler, Twisted won't stomp on it: exarkun@charm:~$ python Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> def f(*a): ... print 'sigint' ... >>> import signal >>> signal.signal(signal.SIGINT, f) <built-in function default_int_handler> >>> from twisted.internet import reactor >>> reactor.run() sigint sigint sigint Quit exarkun@charm:~$ Jean-Paul
Jean-Paul Calderone wrote:
On Wed, 4 Feb 2009 10:32:19 +0000, Reza Lotun <reza@getpeer.com> wrote:
Hi Gabriel,
On Wed, Feb 4, 2009 at 10:15 AM, Gabriel Rossetti <gabriel.rossetti@arimaz.com> wrote:
Hello everyone,
I would like to run some cleanup code when my Twisted app receives a signal (SIGINT/SIGBREAK/SIGTERM).
I saw that "_SignalReactorMixin" sets the handlers and that "ReactorBase" defines the default handlers : ... My question is how can I redefine them, other than monkey patching or inheriting the reactor and over-riding them (which I'd rather not do since some of my code uses the windows reactor when on windows since I was having problems with windows event)? Is there such a mechanism, something like setDefaultSig("SIGINT, mySigIntHandler)?
One easy way to do it is to reactor.run(installSignalHandlers=False) and then manually install your own signal handlers in the usual way (using the python signal library).
If you do this, you'll break spawnProcess. Fortunately, if you just install a SIGINT handler, Twisted won't stomp on it:
exarkun@charm:~$ python Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2 Type "help", "copyright", "credits" or "license" for more information.
def f(*a): ... print 'sigint' ... >>> import signal signal.signal(signal.SIGINT, f) <built-in function default_int_handler> from twisted.internet import reactor reactor.run() sigint sigint sigint Quit exarkun@charm:~$ Jean-Paul
Ok, this is what I have been doing, but I suspect that it is the reason that my reactor won't stop sometimes (when SIGINT is sent). It usually happens with code using threads or wxpython support. I was wondering if my redefining handlers wasn't messing things up somehow... Gabriel
participants (3)
-
Gabriel Rossetti
-
Jean-Paul Calderone
-
Reza Lotun