Popular recipes tagged "lock"http://code.activestate.com/recipes/tags/lock/popular/2015-01-09T10:14:15-08:00ActiveState Code RecipesSystemMutex (Python) 2015-01-09T10:14:15-08:00Fabio Zadroznyhttp://code.activestate.com/recipes/users/4180406/http://code.activestate.com/recipes/578998-systemmutex/ <p style="color: grey"> Python recipe 578998 by <a href="/recipes/users/4180406/">Fabio Zadrozny</a> (<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/filelock/">filelock</a>, <a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/mutex/">mutex</a>). </p> <p>This module provides a way to create a mutex which is valid for the system (i.e.: it can be seen by multiple processes).</p> <p>Note that the mutex is kept until release_mutex() is called or when it's garbage-collected.</p> Distributed 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> Set windows 7 to lock itself (upon timeout) if no internet connection found - security measure (Python) 2012-07-09T20:56:22-07:00commentator8http://code.activestate.com/recipes/users/4182761/http://code.activestate.com/recipes/578200-set-windows-7-to-lock-itself-upon-timeout-if-no-in/ <p style="color: grey"> Python recipe 578200 by <a href="/recipes/users/4182761/">commentator8</a> (<a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/windows/">windows</a>, <a href="/recipes/tags/windows_registry/">windows_registry</a>). Revision 2. </p> <p>This script (or exe if using web2py with """setup(windows=['locker2.py']))""") can be run as a task in windows every x minutes and will test for the presence of an internet connection and depending on whether it is found will set windows to lock after a given timeout without user activity.</p> <p>This was made with help from random code snippets from around the web.</p> <p>Tested only on windows 7.</p> Win32 named mutex class for system-wide mutex (Python) 2011-07-15T21:12:40-07:00Ben Hoythttp://code.activestate.com/recipes/users/4170919/http://code.activestate.com/recipes/577794-win32-named-mutex-class-for-system-wide-mutex/ <p style="color: grey"> Python recipe 577794 by <a href="/recipes/users/4170919/">Ben Hoyt</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/mutex/">mutex</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/win32/">win32</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>NamedMutex is a class for using Windows (Win32) named mutexes for system-wide locks. For example, we use these to lock system-wide log files that multiple processes can write to.</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> How to debug (deadlocked) multi threaded programs (Python) 2010-07-26T15:39:15-07:00Laszlo Nagyhttp://code.activestate.com/recipes/users/4150221/http://code.activestate.com/recipes/577334-how-to-debug-deadlocked-multi-threaded-programs/ <p style="color: grey"> Python recipe 577334 by <a href="/recipes/users/4150221/">Laszlo Nagy</a> (<a href="/recipes/tags/dead/">dead</a>, <a href="/recipes/tags/debug/">debug</a>, <a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/stack/">stack</a>, <a href="/recipes/tags/thread/">thread</a>, <a href="/recipes/tags/trace/">trace</a>). </p> <p>Simple module that allows you to explore deadlocks in multi threaded programs.</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> 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>