title: Putting a thread to sleep until event X occurs
body: <p>I'm writing to many files in a threaded app and I'm creating one handler per file. I have HandlerFactory class that manages the distribution of these handlers. What I'd like to do is that</p>
  
  <p>thread A requests and gets foo.txt's file handle from the HandlerFactory class</p>
  
  <p>thread B requests foo.txt's file handler</p>
  
  <p>handler class recognizes that this file handle has been checked out</p>
  
  <p>handler class puts thread A to sleep</p>
  
  <p>thread B closes file handle using a wrapper method from HandlerFactory</p>
  
  <p>HandlerFactory notifies sleeping threads</p>
  
  <p>thread B wakes and successfully gets foo.txt's file handle</p>
  
  <p>This is what I have so far, </p>
  
  <pre><code>def get_handler(self, file_path, type):
      self.lock.acquire()
      if file_path not in self.handlers:
          self.handlers[file_path] = open(file_path, type)
      elif not self.handlers[file_path].closed:
          time.sleep(1)
      self.lock.release()
      return self.handlers[file_path][type]
  </code></pre>
  
  <p>I believe this covers the sleeping and handler retrieval successfully, but I am unsure how to wake up all threads, or even better wake up a specific thread.</p>