title: Terminating a python thread from within based on a timer
body: <p>I have tried to solve this problem through multiple forums and still have not got a solution to the answer. 
  I'll try to be as specific as possible as to what I am looking for. 
  I have a use case where I need to have a thread terminate itself after a certain time out. I do not want to use .join(timeout=x) from the main thread because that as I understand does not terminate the thread. I want this timed event to be realized within the thread and it should do some clean up and updates before it terminates itself.
  JFYI: I can't use a loop wothin the run method to check for a state. My need is that the target function will be called within the run method.</p>
  
  <pre><code>class MyThread(Thread):
      def __init__(self):
          Thread.__init__(self)
          self.timer = Timer(5.0, self.timeout)
  
      def run(self):
          self.timer.start()
          # call the target function which runs
  
       def timeout(self):
            # timeout code
            # If timeout has been reached then thread should do some internal cleanup and terminate thread here.
  </code></pre>