title: What is the "correct" way to make a stoppable thread in Python, given stoppable pseudo-atomic units of work?
body: <p>I'm writing a threaded program in Python. This program is interrupted very frequently, by user (CRTL+C) interaction, and by other programs sending various signals, all of which should stop thread operation in various ways. The thread does a bunch of units of work (I call them "atoms") in sequence. </p>
  
  <p>Each atom can be stopped quickly and safely, so making the thread itself stop is fairly trivial, but my question is: <strong>what is the "right", or canonical way to implement a stoppable thread, given stoppable, pseudo-atomic pieces of work to be done?</strong> </p>
  
  <p>Should I poll a <code>stop_at_next_check</code> flag before each atom (example below)? Should I decorate each atom with something that does the flag-checking (basically the same as the example, but hidden in a decorator)? Or should I use some other technique I haven't thought of?</p>
  
  <p>Example (simple stopped-flag checking):</p>
  
  <pre><code>class stoppable(Thread):
      stop_at_next_check = False
      current_atom = None
  
      def __init__(self):
          Thread.__init__(self)
  
      def do_atom(self, atom):
          if self.stop_at_next_check:
              return False
          self.current_atom = atom
          self.current_atom.do_work()
          return True     
  
      def run(self):
          #get "work to be done" objects atom1, atom2, etc. from somewhere
  
          if not do_atom(atom1):
              return
  
          if not do_atom(atom2):
              return
  
          #...etc
  
      def die(self):
          self.stop_at_next_check = True
          self.current_atom.stop()
  </code></pre>