title: How to properly try/catch timeout errors when acquiring a lock in Python 3.4
body: <p>I'm using <code>threading.RLock</code> for a multithreaded application. I'd like to have threads try to acquire a lock, and if unsuccessful, retry some number of times provided the time spent retrying is below some timeout threshold.</p>
  
  <p>Here's what I have</p>
  
  <pre><code>&gt;&gt;&gt; import threading
  &gt;&gt;&gt; lock = threading.RLock()
  &gt;&gt;&gt; def worker():
          with lock.acquire(timeout=5):
              print('acquired')
  &gt;&gt;&gt; lock.acquire()
  &gt;&gt;&gt; t2 = threading.Thread(target=worker)
  &gt;&gt;&gt; t2.start()
  </code></pre>
  
  <p>This seems to work, and I get an exception after five seconds of waiting:</p>
  
  <pre><code>&gt;&gt;&gt; Exception in thread Thread-1:
  Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/threading.py", line 920, in _bootstrap_inner
      self.run()
    File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/threading.py", line 868, in run
      self._target(*self._args, **self._kwargs)
    File "&lt;pyshell#7&gt;", line 2, in test
  AttributeError: __exit__
  
  KeyboardInterrupt
  &gt;&gt;&gt; 
  KeyboardInterrupt
  </code></pre>
  
  <p>The <code>AttributeError</code> seems strange - shouldn't we see some kind of a timeout error?</p>
  
  <p>My questions here are:</p>
  
  <ol>
  <li>Am I doing this correctly in Python 3.4? I've been looking at a <a href="https://stackoverflow.com/questions/8392640/how-to-implement-a-lock-with-a-timeout-in-python-2-7">similar question asked on SO</a> that seems targeted to Python 2.7. Moreover, I'm somewhat confused why I'd need to use a condition variable if <code>lock.acquire</code> accepts a timeout argument.</li>
  <li>Is there a better pattern for retrying <code>lock.acquire</code> with a timeout/delay in between retries?</li>
  </ol>