[Twisted-Python] redefine signal handlers

Jean-Paul Calderone exarkun at divmod.com
Wed Feb 4 09:55:24 EST 2009

On Wed, 4 Feb 2009 10:32:19 +0000, Reza Lotun <reza at getpeer.com> wrote:
>Hi Gabriel,
>On Wed, Feb 4, 2009 at 10:15 AM, Gabriel Rossetti
><gabriel.rossetti at arimaz.com> wrote:
>> Hello everyone,
>> I would like to run some cleanup code when my Twisted app receives a signal
>> 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 at 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()
    exarkun at charm:~$ 


More information about the Twisted-Python mailing list