[Twisted-Python] wxPython + Twisted = signals no longer caught

Gabriel Rossetti gabriel.rossetti at arimaz.com
Fri Aug 22 05:42:08 EDT 2008

Hello everyone,

I am writing a (Twisted) program that intercepts some signals using , 
such as Ctrl+C, and stops the reactor. I have a base class that sets-up 
signal handlers and it works great. The handlers are in my base factory 
class (we'll call it MyBaseFactory). Here's the code in the 
MyBaseFactory's __init__() :

    def __init__(self, proto, *args, **kwargs):
        self.protocol = proto
        if signal.getsignal(signal.SIGINT) == signal.default_int_handler:
            # only handle if there isn't already a handler, e.g. for Pdb.
            signal.signal(signal.SIGINT, self._sigHandler)
        signal.signal(signal.SIGTERM, self._sigHandler)
        # Catch Ctrl-Break in windows
        if hasattr(signal, "SIGBREAK"):
            signal.signal(signal.SIGBREAK, self._sigHandler)

    def _sigHandler(self, signum, frame):
        if(signum == signal.SIGINT or signal.SIGTERM):
            reactor.callLater(0, reactor.stop)
        # Catch Ctrl-Break in windows
        elif hasattr(signal, "SIGBREAK") and signum == signal.SIGBREAK:
            reactor.callLater(0, reactor.stop)

I've recently had to integrate wxPython in parts of my application and 
I've inherited from MyBaseFactory and wx.App. When the Gui starts, it 
creates a wx.Dialog descendant/child that creates a wx.TaskBarIcon. I 
use the wxPython integration reactor code. Also, I have tried using 
wx.Frame instead of wx.Dialog but it doesn't change anything.

The interface works great, I can use twisted, I get and receive msgs, 
etc, but when I'm in the terminal were the program was run from, and I 
send SIGINT (Ctrl+C) nothing happens. Does anyone know why wxPython or 
Twisted eats my Ctrl+C in the terminal? This code (MyBaseFactory) works 
without the wxPython (I've been using it for several months).

Thank you,

PS I've also posted before to the wxPython mailing list without 
mentioning the twisted part, but apparently pure wxPython does not do 
this. I've also tested it (removed all the twisted stuff) and it does 
close when a SIGINT is sent.

More information about the Twisted-Python mailing list