title: Python: Timeout Exception Handling with Signal.Alarm
body: <p>I am trying to implement a timeout exception handler if a function call is taking too long.</p>
  
  <p><strong>EDIT</strong>: In fact, I am writing a Python script using subprocess, which calls an old C++ program with arguments. I know that the program hangs from time to time, not returning anything. That's why I am trying to put a time limit and to move on to next call with different argument and etc.</p>
  
  <p>I've been searching and trying to implement it, but it doesn't quite work, so I wish to get some help. What I have so far is:</p>
  
  <pre><code>#! /usr/bin/env python
  
  import signal
  
  class TimeOutException(Exception):
      def __init__(self, message, errors):
          super(TimeOutException, self).__init__(message)
          self.errors = errors
  
  def signal_handler(signum, frame):
      raise TimeOutException("Timeout!")
  
  signal.signal(signal.SIGALRM, signal_handler)
  signal.alarm(3)
  
  try:
      while True:
          pass
  
  except TimeOutException:
      print "Timed out!"
  
  signal.alarm(0)
  </code></pre>
  
  <p><strong>EDIT</strong>: The Error message I receive currently is "TypeError: <strong>init</strong>() takes exactly 3 arguments (2 given)</p>
  
  <p>Also, I would like ask a basic question regarding the except block. what's the role difference between the code right below "except TimeOutException" and the code in the "Exception handler"? It seems both can do the same thing?</p>
  
  <p>Any help would be appreciated.</p>