Popular recipes tagged "thread"http://code.activestate.com/recipes/tags/thread/2016-01-30T00:40:20-08:00ActiveState Code RecipesPyqt / Pyside: thread-safe callbacks + main loop integration (Python) 2013-10-12T08:43:09-07:00Justin Israelhttp://code.activestate.com/recipes/users/4187487/http://code.activestate.com/recipes/578634-pyqt-pyside-thread-safe-callbacks-main-loop-integr/ <p style="color: grey"> Python recipe 578634 by <a href="/recipes/users/4187487/">Justin Israel</a> (<a href="/recipes/tags/events/">events</a>, <a href="/recipes/tags/pyqt/">pyqt</a>, <a href="/recipes/tags/pyside/">pyside</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/thread/">thread</a>). Revision 4. </p> <p>A mechanism for communication from any thread to the main thread.</p> <p>It uses the same custom Event, but adds a classmethod convenience for taking a callback and params, wrapping it into an event and posting it to the receiver. Also adds in support for weak method references to the callback, in case the source object gets deleted before its callback is actually called.</p> <p>Merges forks of both: <a href="http://code.activestate.com/recipes/81253/#c5" rel="nofollow">http://code.activestate.com/recipes/81253/#c5</a> <a href="http://code.activestate.com/recipes/578299-pyqt-pyside-thread-safe-global-queue-main-loop-int/" rel="nofollow">http://code.activestate.com/recipes/578299-pyqt-pyside-thread-safe-global-queue-main-loop-int/</a></p> Basic threaded Python TCP server (Python) 2012-09-19T17:59:01-07:00Luis Martin Gilhttp://code.activestate.com/recipes/users/4183220/http://code.activestate.com/recipes/578247-basic-threaded-python-tcp-server/ <p style="color: grey"> Python recipe 578247 by <a href="/recipes/users/4183220/">Luis Martin Gil</a> (<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/server/">server</a>, <a href="/recipes/tags/tcp/">tcp</a>, <a href="/recipes/tags/thread/">thread</a>). Revision 3. </p> <p>TCP provides reliable, ordered delivery of a stream of bytes from a program on one computer to another program on another computer. Sometimes you need a quick deployment of a TCP server and here I bring to you a Python 2.* snippet of a threaded Python server.</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> 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> 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> Python Thread Pool (Python) 2010-04-12T22:27:32-07:00Emilio Montihttp://code.activestate.com/recipes/users/4173642/http://code.activestate.com/recipes/577187-python-thread-pool/ <p style="color: grey"> Python recipe 577187 by <a href="/recipes/users/4173642/">Emilio Monti</a> (<a href="/recipes/tags/pool/">pool</a>, <a href="/recipes/tags/thread/">thread</a>). Revision 9. </p> <p>A simple Python ThreadPool based on the standard library's <a href="http://docs.python.org/library/queue.html">Queue</a> object.</p> 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> Multicontext (e.g. asynchronous) inline execution framework using coroutines (Python) 2012-12-06T19:32:20-08:00Glenn Eychanerhttp://code.activestate.com/recipes/users/4172294/http://code.activestate.com/recipes/576965-multicontext-eg-asynchronous-inline-execution-fram/ <p style="color: grey"> Python recipe 576965 by <a href="/recipes/users/4172294/">Glenn Eychaner</a> (<a href="/recipes/tags/asynchronous/">asynchronous</a>, <a href="/recipes/tags/coroutine/">coroutine</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/inline/">inline</a>, <a href="/recipes/tags/nonblocking/">nonblocking</a>, <a href="/recipes/tags/pattern/">pattern</a>, <a href="/recipes/tags/thread/">thread</a>). Revision 14. </p> <p>A framework for executing inline code, contained in a generator, across multiple execution contexts, by pairing it with an executor that handles the context switching at each yield. An example of a generator which executes some iterations synchronously and some asynchronously is provided. The framework is general enough to be applied to many different coroutine situations.</p> Thread pool with same API as (multi)processing.Pool (Python) 2016-01-30T00:40:20-08:00david decotignyhttp://code.activestate.com/recipes/users/4129454/http://code.activestate.com/recipes/576519-thread-pool-with-same-api-as-multiprocessingpool/ <p style="color: grey"> Python recipe 576519 by <a href="/recipes/users/4129454/">david decotigny</a> (<a href="/recipes/tags/multiprocessing/">multiprocessing</a>, <a href="/recipes/tags/pool/">pool</a>, <a href="/recipes/tags/processing/">processing</a>, <a href="/recipes/tags/thread/">thread</a>). Revision 10. </p> <p>There are probably &lt;write your guess here&gt;s of recipes presenting how to implement a pool of threads. Now that multiprocessing is becoming mainstream, this recipe takes multiprocessing.Pool as a model and re-implements it entirely with threads. Even the comments should look familiar... This recipe also adds 2 new methods: imap_async() and imap_unordered_async().</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> Interval Execute a function (Python) 2009-04-24T08:58:27-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/576726-interval-execute-a-function/ <p style="color: grey"> Python recipe 576726 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/thread/">thread</a>, <a href="/recipes/tags/timer/">timer</a>). Revision 4. </p> <p>Here I just want to keep it simple. If you want to execute a function every n second, this function should be competent. However, please note that the task is actually executed in another thread. </p> Multithreaded Multiplexer/Demultiplexer to allow multiple transactions to be performed in parallel on a single channel (Python) 2008-10-04T00:12:00-07:00david decotignyhttp://code.activestate.com/recipes/users/4129454/http://code.activestate.com/recipes/576520-multithreaded-multiplexerdemultiplexer-to-allow-mu/ <p style="color: grey"> Python recipe 576520 by <a href="/recipes/users/4129454/">david decotigny</a> (<a href="/recipes/tags/demultiplexer/">demultiplexer</a>, <a href="/recipes/tags/multiplexer/">multiplexer</a>, <a href="/recipes/tags/thread/">thread</a>, <a href="/recipes/tags/transaction/">transaction</a>). Revision 8. </p> <p>Imagine you have one wire and you want to send a message from a machine A to another machine B, and wait for an answer from B (a "request/response transaction" in the following). Now imagine you have many threads on A which can make this kind of transaction "simultaneously", then you have to be cautious, otherwise you might get the wrong responses for your transactions. One solution is to serialize the transactions: the threads on A will have to wait for their turn to make their request/response transaction.</p> <p>Another solution is to map a thread requesting a transaction on A, to a thread handling it on B. This allows for several transactions to be in flight at the same time on the same wire. This is what this module does: on A lives a multiplexer and threads call its transaction() method to initiate the request/response. On B, process_transaction() of a demultiplexer will get called by worker threads. All this in a multi-threaded way on a single wire. The module takes care of managing the interleaving requests/responses.</p>