Popular recipes tagged "concurrency"http://code.activestate.com/recipes/tags/concurrency/popular/2014-06-30T20:30:57-07:00ActiveState Code RecipesNon-blocking readlines() (Python) 2014-06-30T20:30:57-07:00Zack Weinberghttp://code.activestate.com/recipes/users/4190298/http://code.activestate.com/recipes/578900-non-blocking-readlines/ <p style="color: grey"> Python recipe 578900 by <a href="/recipes/users/4190298/">Zack Weinberg</a> (<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/input/">input</a>, <a href="/recipes/tags/line/">line</a>, <a href="/recipes/tags/nonblocking/">nonblocking</a>). </p> <p>A generator function which takes a file object (assumed to be some sort of pipe or socket, open for reading), and yields lines from it without blocking. If there is no input available, it will yield an endless stream of empty strings until input becomes available again; caller is responsible for not going into a busy loop. (Newlines are normalized but not stripped, so if there is actually a blank line in the input, the value yielded will be <code>'\n'</code>.) The intended use case is a thread which must respond promptly to input from a pipe, and also something else which cannot be fed to <code>select</code> (e.g. a <code>queue.Queue</code>). Note that the file object is ignored except for its <code>fileno</code>.</p> <p>Only tested on Unix. Only tested on 3.4; ought to work with any python that has <code>bytearray</code>, <code>locale.getpreferredencoding</code>, and <code>fcntl</code>.</p> A multithreaded, concurrent version of map() (Python) 2010-08-16T23:04:48-07:00Wai Yip Tunghttp://code.activestate.com/recipes/users/2382677/http://code.activestate.com/recipes/577360-a-multithreaded-concurrent-version-of-map/ <p style="color: grey"> Python recipe 577360 by <a href="/recipes/users/2382677/">Wai Yip Tung</a> (<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/multithreading/">multithreading</a>, <a href="/recipes/tags/threads/">threads</a>). </p> <p>map() applies a function to a list of data sequentially. This is a variation to map that execute each function call concurrently in a thread.</p> Git Shell Script to enhance inline automation script... (Bash) 2010-11-01T00:43:03-07:00Patrick Riendeauhttp://code.activestate.com/recipes/users/4175653/http://code.activestate.com/recipes/577447-git-shell-script-to-enhance-inline-automation-scri/ <p style="color: grey"> Bash recipe 577447 by <a href="/recipes/users/4175653/">Patrick Riendeau</a> (<a href="/recipes/tags/code/">code</a>, <a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/git/">git</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/oriented/">oriented</a>, <a href="/recipes/tags/programs/">programs</a>, <a href="/recipes/tags/property/">property</a>, <a href="/recipes/tags/property_creation/">property_creation</a>, <a href="/recipes/tags/repository/">repository</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/shelve/">shelve</a>). </p> <p>This tiny effort, depend from Fnct.D ActiveState no.577446 Core, also available from github <a href="http://github.com/priendeau/Fnct.d" rel="nofollow">http://github.com/priendeau/Fnct.d</a>, can develop basic methodology of implementing oriented program within uses of function re-declaration with function-parser to create both property-function to discover uses of getter-function and setter-function</p> Multiprocess-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> 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> Generator Attributes (Python) 2010-02-18T17:23:59-08:00Longpokehttp://code.activestate.com/recipes/users/4173085/http://code.activestate.com/recipes/577057-generator-attributes/ <p style="color: grey"> Python recipe 577057 by <a href="/recipes/users/4173085/">Longpoke</a> (<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/state/">state</a>). </p> <p>A decorator that creates a generator factory that creates generators that can have attributes set on them (like a normal python class).</p> Concurrent buffer for generators (Python) 2010-05-05T22:47:33-07:00Javier Ruerehttp://code.activestate.com/recipes/users/4172765/http://code.activestate.com/recipes/576999-concurrent-buffer-for-generators/ <p style="color: grey"> Python recipe 576999 by <a href="/recipes/users/4172765/">Javier Ruere</a> (<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/threading/">threading</a>). Revision 2. </p> <p>A buffer that will partially consume an iterator in the background.</p> <p>Very useful for reading files and merging the data using the excellent <a href="http://code.activestate.com/recipes/491285/" rel="nofollow">http://code.activestate.com/recipes/491285/</a></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>