Most viewed recipes tagged "file_lock"http://code.activestate.com/recipes/tags/file_lock/views/2010-09-22T17:30:10-07:00ActiveState Code RecipesMultiprocess-safe logging file-handler + interprocess RLock (Python)
2010-09-22T17:30:10-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577395-multiprocess-safe-logging-file-handler-interproces/
<p style="color: grey">
Python
recipe 577395
by <a href="/recipes/users/4172762/">Jan Kaliszewski</a>
(<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/file_lock/">file_lock</a>, <a href="/recipes/tags/flock/">flock</a>, <a href="/recipes/tags/handler/">handler</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/rlock/">rlock</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/threadsafe/">threadsafe</a>).
Revision 11.
</p>
<p>A Python 2.x/3.x-compatibile <strong>multiprocess-safe logging file-handler</strong> (logging.FileHandler replacement, designed for logging to a single file from multiple independent processes) together with a simple <strong>interprocess recursive lock</strong> -- universal abstract classes + Unix/Linux implementation.</p>
<p><strong>Update:</strong> It's is a deeply revised version. Especially, now it --</p>
<ul>
<li>is Python 2.4, 2.5, 2.6, 3.1 -compatibile (previously Py>=2.6 was needed); probably works also with 2.7, 3.0 and 3.2 (but not tested if it does);</li>
<li>is multiprocess-safe as well as thread-safe (proviously thread safety within a process was missed);</li>
<li>is based on public interfaces only (previously FileHandler._open() was called and overriden);</li>
<li>implement full RLock instance interface, as documented for threading.RLock (previously non-blocking mode and context-manager interface were missing).</li>
</ul>
<p>The module contains:</p>
<ul>
<li>Unix/Linux-only example implementation (with flock-based locking):
<strong>FLockRLock</strong> and <strong>FLockFileHandler</strong> classes.</li>
<li>universal abstract classes -- which may be useful at developing implementation for non-Unix platforms:
<strong>MultiprocessRLock</strong>, <strong>MultiprocessFileHandler</strong>, <strong>LockedFileHandler</strong>,</li>
</ul>
<p>Also a quick-and-dirty test was added.</p>
<p><strong>It is still an alpha version -- I'll be very grateful for any feedback.</strong></p>
<hr />
<p><strong>Further updates:</strong></p>
<ul>
<li><p>2010-09-20: Some corrections, especially: non-blocking mode bug in MultiprocessRLock.acquire() fixed; _test() function improved; plus fixes in the description below.</p></li>
<li><p>2010-09-22: _test() improved and moved to description section. Mistaken copyright-notice removed.</p></li>
</ul>
fcntl.flock() (Unix file lock) behaviour sampling script (Python)
2010-09-22T00:11:12-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577404-fcntlflock-unix-file-lock-behaviour-sampling-scrip/
<p style="color: grey">
Python
recipe 577404
by <a href="/recipes/users/4172762/">Jan Kaliszewski</a>
(<a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/file_descriptor/">file_descriptor</a>, <a href="/recipes/tags/file_lock/">file_lock</a>, <a href="/recipes/tags/flock/">flock</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/thread/">thread</a>).
Revision 3.
</p>
<p>A quick <em>fcntl.flock(fcntl.LOCK_EX | fcntl.LOCK_NB)</em> call sampling script: with <em>one file object</em> (and descriptor) or <em>separate file objects</em> (and different descriptors) pointing to the same filesystem path -- with/without <strong>threading</strong> or <strong>forking</strong>.</p>
<p>It's rather exemplum-and-educational piece of code than utility-script, unless somebody has to few slots in their memory to remember that <strong>flock</strong> is file-descriptor-tacked (then quick run of the script can save web-searching) :)</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>