Popular recipes tagged "locking"http://code.activestate.com/recipes/tags/locking/popular/2012-07-04T21:03:32-07:00ActiveState Code RecipesDistributed lock manager for Python (Python) 2012-07-04T21:03:32-07:00pavelhttp://code.activestate.com/recipes/users/4171837/http://code.activestate.com/recipes/578194-distributed-lock-manager-for-python/ <p style="color: grey"> Python recipe 578194 by <a href="/recipes/users/4171837/">pavel</a> (<a href="/recipes/tags/distributed/">distributed</a>, <a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/locking/">locking</a>, <a href="/recipes/tags/locks/">locks</a>, <a href="/recipes/tags/manager/">manager</a>, <a href="/recipes/tags/socket/">socket</a>, <a href="/recipes/tags/thread/">thread</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/wsgi/">wsgi</a>). Revision 2. </p> <p>Distributed lock manager provides mutex(es) over network. It is used to synchronize processes running on different machines, e.g. WSGI processes in case of web applications. Lock object is compatible with threading.Lock and can be used as a context manager ("with statement"). It can be easily modified to use UNIX sockets instead of TCP/IP. Communication protocol is text based.</p> <p>First start server process:</p> <pre class="prettyprint"><code>$ chmod +x dlm.py $ ./dlm.py </code></pre> <p>Usage:</p> <pre class="prettyprint"><code>from dlm import LockClient client = LockClient('localhost', 27272, 'client_name') lock = client.mkLock('lock_name') lock.acquire() # critical section here... lock.release() # using context manager with lock: # critical section here... </code></pre> Reader-Writer lock with priority for writers (Python) 2011-09-28T21:45:04-07:00Mateusz Koboshttp://code.activestate.com/recipes/users/4178730/http://code.activestate.com/recipes/577803-reader-writer-lock-with-priority-for-writers/ <p style="color: grey"> Python recipe 577803 by <a href="/recipes/users/4178730/">Mateusz Kobos</a> (<a href="/recipes/tags/locking/">locking</a>, <a href="/recipes/tags/threads/">threads</a>). Revision 2. </p> <p>The following class implements a reader-writer lock to use in the second readers-writers problem with python threads. In this problem, many readers can simultaneously access a share, and a writer has an exclusive access to this share. Additionally, the following constraints should be met: 1) no reader should be kept waiting if the share is currently opened for reading unless a writer is also waiting for the share, 2) no writer should be kept waiting for the share longer than absolutely necessary.</p> Simple Lock Classes Based on threading.Lock() (Python) 2011-10-19T21:26:39-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577914-simple-lock-classes-based-on-threadinglock/ <p style="color: grey"> Python recipe 577914 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/locking/">locking</a>). </p> <p>Nothing fancy here. Locks can take many forms. Here are a few.</p> Fast, re-entrant, optimistic lock implemented in Cython (Python) 2010-07-27T20:25:20-07:00Stefan Behnelhttp://code.activestate.com/recipes/users/4174506/http://code.activestate.com/recipes/577336-fast-re-entrant-optimistic-lock-implemented-in-cyt/ <p style="color: grey"> Python recipe 577336 by <a href="/recipes/users/4174506/">Stefan Behnel</a> (<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/locking/">locking</a>). Revision 3. </p> <p>This is a C-level implementation of a fast, re-entrant, optimistic lock for CPython. It is written in Cython. Under normal conditions, it is about 10x faster than threading.RLock because it avoids all locking unless two or more threads try to acquire it at the same time. Under congestion, it is still about 10% faster than RLock due to being implemented in Cython.</p> Application lock (Python) 2009-08-22T13:00:35-07:00Max Polkhttp://code.activestate.com/recipes/users/4171523/http://code.activestate.com/recipes/576891-application-lock/ <p style="color: grey"> Python recipe 576891 by <a href="/recipes/users/4171523/">Max Polk</a> (<a href="/recipes/tags/file_lock/">file_lock</a>, <a href="/recipes/tags/locking/">locking</a>). </p> <p>Ensures application runs only once.</p> Simple shelve with Linux file locking (Python) 2008-12-21T05:50:07-08:00Michael Ihdehttp://code.activestate.com/recipes/users/4168518/http://code.activestate.com/recipes/576591-simple-shelve-with-linux-file-locking/ <p style="color: grey"> Python recipe 576591 by <a href="/recipes/users/4168518/">Michael Ihde</a> (<a href="/recipes/tags/dynamic_method/">dynamic_method</a>, <a href="/recipes/tags/locking/">locking</a>, <a href="/recipes/tags/shelve/">shelve</a>). </p> <p>The shelve module is a easy way to add persistence to your application via a DBM database. However, if you have multiple reader/writer combination you need to lock the file to prevent corruption. The shelve module itself does not provide locking because it is platform specific. If you only need Linux, this simple module provide an easy way to support locking using dynamically added methods.</p> Simple lock-queue via Memcached (Python) 2008-11-19T01:32:07-08:00Tal Einathttp://code.activestate.com/recipes/users/2892534/http://code.activestate.com/recipes/576567-simple-lock-queue-via-memcached/ <p style="color: grey"> Python recipe 576567 by <a href="/recipes/users/2892534/">Tal Einat</a> (<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/locking/">locking</a>, <a href="/recipes/tags/memcache/">memcache</a>, <a href="/recipes/tags/memcached/">memcached</a>, <a href="/recipes/tags/queue/">queue</a>). </p> <p>A simple lock-queue (FIFO) context manager implemented with <a href="http://www.danga.com/memcached/">Memcached</a>.</p> <p>In essence this is a normal lock, where the requests to acquire the lock are granted in the order in which they were originally made. Note that requests to acquire the lock are always blocking.</p>