title: Signal handler inside a class
body: <p>I am trying to write a class to handle signals using the <a href="https://docs.python.org/2/library/signal.html#module-signal" rel="noreferrer">signal</a> python module. The reason for having a class is to avoid the use of globals. This is the code I came up with, but unfortunately it is not working:</p>
  
  <pre><code>import signal
  import constants
  
  
  class SignalHandler (object):
      def __init__(self):        
          self.counter = 0
          self.break = False
          self.vmeHandlerInstalled = False
  
      def setVmeHandler(self):        
          self.vmeBufferFile = open('/dev/vme_shared_memory0', 'rb')
          self.vmeHandlerInstalled = True
  
          signal.signal(signal.SIGUSR1, self.traceHandler)
          signal.siginterrupt(signal.SIGUSR1, False)
          #...some other stuff...
  
  
      def setBreakHandler(self):
          signal.signal(signal.SIGINT, self.newBreakHandler)
          signal.siginterrupt(signal.SIGINT, False)
  
      def newBreakHandler(self, signum, frame):        
          self.removeVMEHandler()
          self.break = True
  
      def traceHandler(self, signum, frame):
          self.counter += constants.Count        
  
      def removeVMEHandler(self):    
          if not self.vmeHandlerInstalled: return
          if self.vmeBufferFile is None: return
  
          signal.signal(signal.SIGUSR1, signal.SIG_DFL)
  
          self.vmeHandlerInstalled = False
  </code></pre>
  
  <p>On the main program I use this class in the following way:</p>
  
  <pre><code>def run():
      sigHandler = SignalHandler()
  
      sigHandler.setBreakHandler()
      sigHandler.setVmeHandler()
  
      while not sigHandler.break:
          #....do some stuff
          if sigHandler.counter &gt;= constants.Count:
              #...do some stuff
  </code></pre>
  
  <p>This solution is not working, as it appears that the handler for the <code>signal.SIGUSR1</code> installed in the <code>setVmeHandler</code> method never gets called.</p>
  
  <p>So my question is: is it possible to handle signal inside a class or shall I use globals?</p>