Popular recipes tagged "locks"http://code.activestate.com/recipes/tags/locks/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>