title: Thread that I can pause and resume?
body: <p>I'm trying to create a thread, that does stuff in the background. I need to be able to effectively 'pause' it when I need to and 'resume' it again later. Also, if the thread is in the middle of doing something when I 'pause' it, it should make the calling thread wait until it finishes what it's doing.</p>
  
  <p>I'm pretty new to Multithreading in Python, so I haven't gotten all that far.</p>
  
  <p>What I have pretty much does everything except make the calling thread wait if pause is called while my thread is doing something.</p>
  
  <p>Here's the outline of what I'm trying to achieve in code:</p>
  
  <pre><code>import threading, time
  
  class Me(threading.Thread):
      def __init__(self):
          threading.Thread.__init__(self)
          #flag to pause thread
          self.paused = False
  
      def run(self):
          while True:
              if not self.paused:
                  #thread should do the thing if
                  #not paused
                  print 'do the thing'
                  time.sleep(5)
  
      def pause(self):
          self.paused = True
          #this is should make the calling thread wait if pause() is
          #called while the thread is 'doing the thing', until it is
          #finished 'doing the thing'
  
      #should just resume the thread
      def resume(self):
          self.paused = False
  </code></pre>
  
  <p>I think I basically need a locking mechanism, but within the same thread?</p>