[Twisted-Python] redefine signal handlers

Gabriel Rossetti gabriel.rossetti at arimaz.com
Wed Feb 4 09:19:43 MST 2009


Jean-Paul Calderone wrote:
> 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
>>> (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 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()
>    sigint
>    sigint
>    sigint
>    Quit
>    exarkun at 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




More information about the Twisted-Python mailing list