Popular recipes tagged "threading" but not "python"http://code.activestate.com/recipes/tags/threading-python/2015-05-19T19:31:59-07:00ActiveState Code RecipesRun OS command with timeout on a list of files using several threads (Python)
2015-05-19T19:31:59-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/579056-run-os-command-with-timeout-on-a-list-of-files-usi/
<p style="color: grey">
Python
recipe 579056
by <a href="/recipes/users/4182514/">Antoni Gual</a>
(<a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/subprocess/">subprocess</a>, <a href="/recipes/tags/threading/">threading</a>).
Revision 3.
</p>
<p>This hack runs a command sequentially on a list of files using two simultaneous threads. If one of the commands takes more than a set time, it's killed and the program goes for the next file. EDITED to add some exception handling.</p>
Primitive Peer to Peer Chat (Python)
2013-07-07T02:09:57-07:00teddy_khttp://code.activestate.com/recipes/users/4187115/http://code.activestate.com/recipes/578591-primitive-peer-to-peer-chat/
<p style="color: grey">
Python
recipe 578591
by <a href="/recipes/users/4187115/">teddy_k</a>
(<a href="/recipes/tags/chat/">chat</a>, <a href="/recipes/tags/peer/">peer</a>, <a href="/recipes/tags/select/">select</a>, <a href="/recipes/tags/socket/">socket</a>, <a href="/recipes/tags/threading/">threading</a>).
</p>
<p>This took me longer than it should have. I am putting it here in the hopes that this post will spare others from having to Google things for extended periods of time.</p>
<p>In short, you either listen for a connection (Chat_Server), or connect to a remote IP address (Chat_Client). From there, you can send text strings back and forth. </p>
<p>This is a bit rough-hewn, obviously; I apologize in advance.</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>
threadbox.py (Python)
2012-10-09T22:40:09-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578152-threadboxpy/
<p style="color: grey">
Python
recipe 578152
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/threading/">threading</a>).
Revision 2.
</p>
<p>Provide a way to run instance methods on a single thread.</p>
<p>This module allows hierarchical classes to be cloned so that their instances
run on one thread. Method calls are automatically routed through a special
execution engine. This is helpful when building thread-safe GUI code.</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>
actorish decorator for making async code look more like sync one and a less blocking (Python)
2011-10-30T19:59:44-07:00Przemyslaw Podczasihttp://code.activestate.com/recipes/users/4179716/http://code.activestate.com/recipes/577931-actorish-decorator-for-making-async-code-look-more/
<p style="color: grey">
Python
recipe 577931
by <a href="/recipes/users/4179716/">Przemyslaw Podczasi</a>
(<a href="/recipes/tags/actor/">actor</a>, <a href="/recipes/tags/threading/">threading</a>).
</p>
<p>I like how gevent is making async code to look like sync but non blocking without all the ugly callbacks.
I tried doing that with threads and object proxy (I found great one at: <a href="http://pypi.python.org/pypi/ProxyTypes" rel="nofollow">http://pypi.python.org/pypi/ProxyTypes</a> written by Phillip J. Eby, and this is where the actual magic happens).</p>
<p>For every function that is decorated it returns a proxy and the io call (or anything else) won't block until the value is actually needed.
(should be some pools and args pickling there, to make it more like message passing but I didn't want to fuzzy the example)
To use it as actor model, I guess it would require to queue requests to decorated object's methods and create a single thread to process them an in LazyProxy callback set q.get() instead of t.join()</p>
Future decorator in a pretty pythonic way (Python)
2011-10-20T16:51:48-07:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/577915-future-decorator-in-a-pretty-pythonic-way/
<p style="color: grey">
Python
recipe 577915
by <a href="/recipes/users/4174931/">Filippo Squillace</a>
(<a href="/recipes/tags/threading/">threading</a>).
</p>
<p>Without thinking in thread creation the idea is to call several times
a function assigning a thread for each call with related parameters
and returning the list of results in a pretty pythonic way.
For example, if we have already defined the function 'func':
res = func(par)
we want to call that several times and get the values in the future. So:
func(par1)
func(par2)
func(par3).get()
func(par4).get_all()</p>
<p>assigning a thread for each call. The 'get' method returns the first possible results,
and 'get_all' returns all the values in a list. The decorator works fine with kwargs too.</p>
<p>This recipe is based on:
<a href="http://code.activestate.com/recipes/355651-implementing-futures-with-decorators/" rel="nofollow">http://code.activestate.com/recipes/355651-implementing-futures-with-decorators/</a>
that use only one call for the function. The problem in that recipe is that each call blocks the execution.</p>
<p>Vote if you like it!</p>
Multi-threaded Mandelbrot Fractal (Python)
2011-05-01T17:33:20-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577680-multi-threaded-mandelbrot-fractal/
<p style="color: grey">
Python
recipe 577680
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/thread/">thread</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/threads/">threads</a>).
Revision 3.
</p>
<p>Multi-threaded Mandelbrot Fractal.</p>
Command Threads (Python)
2010-12-25T23:23:49-08:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577505-command-threads/
<p style="color: grey">
Python
recipe 577505
by <a href="/recipes/users/4168519/">Shao-chuan Wang</a>
(<a href="/recipes/tags/threading/">threading</a>).
Revision 4.
</p>
<p>A thread that accepts callable objects as "commands" or "jobs" to execute.</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>
Decoupled Proxy of a State Bearing Object in a Multiprocessing Environment (Python)
2010-04-08T04:36:05-07:00Mateyuzohttp://code.activestate.com/recipes/users/4172453/http://code.activestate.com/recipes/577180-decoupled-proxy-of-a-state-bearing-object-in-a-mul/
<p style="color: grey">
Python
recipe 577180
by <a href="/recipes/users/4172453/">Mateyuzo</a>
(<a href="/recipes/tags/access/">access</a>, <a href="/recipes/tags/multiprocessing/">multiprocessing</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/processing/">processing</a>, <a href="/recipes/tags/subprocess/">subprocess</a>, <a href="/recipes/tags/synchronization/">synchronization</a>, <a href="/recipes/tags/syncmanager/">syncmanager</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/__getattribute__/">__getattribute__</a>).
Revision 2.
</p>
<p>Describes a simple example of the potential for shared Singleton or Borg object methods to be proxy'd by an object that SyncManger can present to requesting subprocesses.</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>
Thread pool (grows on demand) (Python)
2009-11-03T02:28:12-08:00Pepe Aracilhttp://code.activestate.com/recipes/users/4171769/http://code.activestate.com/recipes/576910-thread-pool-grows-on-demand/
<p style="color: grey">
Python
recipe 576910
by <a href="/recipes/users/4171769/">Pepe Aracil</a>
(<a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/threads/">threads</a>, <a href="/recipes/tags/thread_pool/">thread_pool</a>).
Revision 3.
</p>
<p>Thread pool that grows on demand.</p>
Simple threading decorator (Python)
2009-03-10T01:38:51-07:00david.gaarenstroomhttp://code.activestate.com/recipes/users/4168848/http://code.activestate.com/recipes/576684-simple-threading-decorator/
<p style="color: grey">
Python
recipe 576684
by <a href="/recipes/users/4168848/">david.gaarenstroom</a>
(<a href="/recipes/tags/async/">async</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/run_asynchronously/">run_asynchronously</a>, <a href="/recipes/tags/thread/">thread</a>, <a href="/recipes/tags/threading/">threading</a>).
Revision 4.
</p>
<p>When you're new at threading, using threads can be a bit daunting at first. If all you want is just to "run this function in parallel (= asynchronously) to the main program code", then this recipe can be of use. Simply use "@run_async" as a decorator for the function you want to run asynchronously. A call to that function will return immediately but the function itself will run in parallel.</p>
pyGTK threading (Python)
2009-04-01T07:07:56-07:00oohay_email2004http://code.activestate.com/recipes/users/4169723/http://code.activestate.com/recipes/576705-pygtk-threading/
<p style="color: grey">
Python
recipe 576705
by <a href="/recipes/users/4169723/">oohay_email2004</a>
(<a href="/recipes/tags/pygtk/">pygtk</a>, <a href="/recipes/tags/threading/">threading</a>).
Revision 2.
</p>
<p>This is my first successful attempt at threading in pyGTK.</p>
<p>Suggestions please.</p>
Multithreaded FIFO Gate (Python)
2008-09-08T08:29:14-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/576418-multithreaded-fifo-gate/
<p style="color: grey">
Python
recipe 576418
by <a href="/recipes/users/760763/">Anand</a>
(<a href="/recipes/tags/semaphores/">semaphores</a>, <a href="/recipes/tags/synchronization/">synchronization</a>, <a href="/recipes/tags/system/">system</a>, <a href="/recipes/tags/threading/">threading</a>).
Revision 5.
</p>
<p>While programming with multiple threads, sometimes one needs a construct which allows to suspend the execution of a set of running threads. This is normally required by an outside thread which wants to suspend the running threads for performing a specific action. The threads need to resume after the action in the same order in which they got suspended. </p>
<p>A thread gate (or gateway) allows you to do this. It acts like a gate through which only one thread can pass at a time. By default the gate is open, allowing all threads to "enter" the gate. When a thread calls "close", the gate is closed, blocking any threads which make a further call to "enter", till the gate is re-opened by the owner, whence the threads resume the order in which they got blocked.</p>
<p>The real-life parallel for this is a human operated level cross, which allows only one vehicle to pass at a time.</p>