title: How to safely stop Python threads with key input
body: <p>In my application I have two threads. Main one and &quot;thread&quot;. therad generates some data and stores it in a python list. The main thread periodically copies the content of the list generated by &quot;thread&quot;. Both threads have an infinite while loop. My goal is stopping both threads when I press any key+enter. To achieve this goal, the program must wait for a keyboard input while the threads are running. I thought I need another thread (lets say manager) which is only waiting for a keyboard input during execution. Here is what I tried first:</p>
  <pre><code>class managerThread(threading.Thread):
      is_running = True
      def __init__(self):
          threading.Thread.__init__(self)
          signal.signal(signal.SIGINT, self.kill_all)
          signal.signal(signal.SIGTERM, self.kill_all)
        
      def run(self):
          input(&quot;Press any key+enter to stop: &quot;)
          self.is_running = False
      
      def kill_all(self,signum, frame):
          print(&quot;Process ended with keyboard interrupt&quot;)
          self.is_running = False
          sys.exit(-1)
  
          
  class thread(threading.Thread):
      mgr = managerThread()
      mgr.start()
      def __init__(self):
          threading.Thread.__init__(self)
      
      def run(self):
          while (self.mgr.is_running):
              print(&quot;this is acquiring data&quot;)
              sleep(2.5)
              
          self.mgr.join()
          print(&quot;manager stopped&quot;)
  
  
  if __name__ == &quot;__main__&quot;:
      thread = thread()
      thread.start()
      while (thread.mgr.is_running):
          print(&quot;this is copying data&quot;)
          sleep(3)
          
      thread.join()
      print(&quot;thread is stopped&quot;)
      sys.exit(0)
  </code></pre>
  <p>Above code is doing exactly what I want to do. Bu this does not seem correct. The manager manages all the others but it is created in one of the slave threads. Another problem is one may try to create multiple managers in different threads. This is something must be strictly avoided. Then I thought the manager must be inherited by the managed classes. Here is what I tried:</p>
  <pre><code>class managerThread(threading.Thread):
      is_running = True
      def __init__(self):
          threading.Thread.__init__(self)
          signal.signal(signal.SIGINT, self.kill_all)
          signal.signal(signal.SIGTERM, self.kill_all)
          self.start()
        
      def run(self):
          input(&quot;Press any key+enter to stop: &quot;)
          self.is_running = False
      
      def kill_all(self,signum, frame):
          print(&quot;Process ended with keyboard interrupt&quot;)
          self.is_running = False
          sys.exit(-1)
  
          
  class thread(managerThread):
      def __init__(self):
          super().__init__()
          threading.Thread.__init__(self)
      
      def run(self):
          while (self.is_running):
              print(&quot;this is acquiring data&quot;)
              sleep(2.5)
              
          print(&quot;manager stopped&quot;)
  
  
  if __name__ == &quot;__main__&quot;:
      thread = thread()
      thread.start()
      while (thread.is_running):
          print(&quot;this is copying data&quot;)
          sleep(3)
          
      thread.join()
      print(&quot;thread is stopped&quot;)
      sys.exit(0)
  </code></pre>
  <p>As seen in the second code the major part is the same. I tried to make <code>thread</code> as a child of <code>managerThread</code>. However this is not working. The manager never executes &quot;run&quot; method. So I cannot stop the other threads. Another crucial problem is I do not how to stop <code>super()</code> with <code>join()</code>. I am sure I am doing a mistake about class inheritance but I could not resolve the problem since I do not have too much experience with OOP and threads doubled my confusion.
  <br/><strong>Note:</strong> I do not care about synchronization of the threads.
  <br/>My questions are:
  <br/>- Is creating a manager thread correct to safely stop the slave threads? If not, what is the proper way?
  <br/>- Why the second code is not working? What do I have to modify to get it work?
  <br/>- Why the parent class is initializing but it is never running &quot;run&quot; method?
  <br/>- I believe that the parent class is never starting but how can I stop it in the second code if it is actually starting?
  <br/> Thank you.</p>