title: Python socketserver - interaction with threading.Lock()?
body: <p>I have used the SocketServer module to create a small server. It basically works, but I have some questions regarding multiple clients accesing the server simultaneously:</p>
  
  <ol>
  <li><p>When using the absolutely basic TCP SocketServer, i.e. without ThreadingMixin or ForkingMixin - what happens when a second client tries to connect to the server before it is complete serving the first client; will the second connection be dropped - or will the second client block?</p></li>
  <li><p>I have tried adding the ThreadingMixin. In that case I need to protect a certain code block in the server code with a lock</p></li>
  </ol>
  
  <pre>
  lock = threading.Lock()
  with lock:
     # Do stuff here ...
     # ...
  
  # Continue in unlocked scope.
  </pre>
  
  <p>but the lock is seemingly ignored - and the whole thing crashes. Instead of the with I have also tried using lock.aquire() &amp; lock.release() - then it seems to block unconditionally on lock.aquire(). So to me it seems the ThreadingMixin does not play very nicely with threading.Lock()?   </p>
  
  <p>Edit: Dan's comment about using the same lock is probably key to my locking problems; my current handler code looks like this:</p>
  
  <pre><code>class MyThreadedHandler(SocketServer.BaseRequestHandler):
  
     def handle(self):
        lock = threading.Lock()     &lt;-- Each thread a separate lock?!
        with lock:
            # Do serial stuff
        # Continue parallell processing.
  </code></pre>
  
  <p>So - I guess each thread will lock it's personal lock - not very much point!</p>