title: How to create a synchronized function across all instances
body: <p>I want to create a synchronized method in python with respect to  some CRUD functions on all instances of a class. For example while create is called and ongoing by a thread, delete needs to wait on the same object. </p>
  
  <p>Can someone please tell me if the code below is correct. I may have some syntax error but what I want to know is if the lock is going to be the same across calls to create ALL instances of this class and therefore if any instance create/delete is in progress delete/create on the same or other instances by another thread will have to wait? </p>
  
  <pre><code>import threading
  import functools
  
  def synchronized(wrapped):
      lock = threading.Lock()
      @functools.wraps(wrapped)
      def _wrap(*args, **kwargs):
          with lock:
              return wrapped(*args, **kwargs)
      return _wrap
  
  class AtomicCRUD(object):
  
      @synchronized
      def create(self):
          #Do stuff that may take a while here. 
          pass
  
      @synchronized
      def delete(self):
         #Do stuff that may take a while here. 
         pass 
  </code></pre>
  
  <p>My understanding of python is synchronized will be called for each of create/delete function object. <strong>I put a print statement in the synchronized function above for the lock object</strong> and did a test run using the following:</p>
  
  <pre><code>@synchronized
  def test1():
      print "test1"
  
  
  @synchronized
  def test2():
      print "test2"
  </code></pre>
  
  <p>And I get the following output, which makes me think the lock used is the same for both function objects. I don't seem to understand how this works.</p>
  
  <pre><code>&lt;Semaphore c=1 _w[0]&gt;
  &lt;Semaphore c=1 _w[0]&gt;
  test1
  test2
  </code></pre>