Popular Python recipes tagged "meta:requires=thread"http://code.activestate.com/recipes/langs/python/tags/meta:requires=thread/2016-11-01T02:27:13-07:00ActiveState Code RecipesPersistent Queue (Python) 2015-11-12T06:45:12-08:00zhangkaizhaohttp://code.activestate.com/recipes/users/4189594/http://code.activestate.com/recipes/579124-persistent-queue/ <p style="color: grey"> Python recipe 579124 by <a href="/recipes/users/4189594/">zhangkaizhao</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>A class for persistent queues.</p> Ordered Dictionary for Py2.4 (Python) 2011-04-24T03:20:45-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576693-ordered-dictionary-for-py24/ <p style="color: grey"> Python recipe 576693 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/linked_list/">linked_list</a>, <a href="/recipes/tags/ordered/">ordered</a>). Revision 9. </p> <p>Drop-in substitute for Py2.7's new collections.OrderedDict. The recipe has big-oh performance that matches regular dictionaries (amortized O(1) insertion/deletion/lookup and O(n) iteration/repr/copy/equality_testing).</p> Calendar Maker (Python) 2012-07-03T05:23:57-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578187-calendar-maker/ <p style="color: grey"> Python recipe 578187 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/archive/">archive</a>, <a href="/recipes/tags/calendar/">calendar</a>, <a href="/recipes/tags/cgi/">cgi</a>, <a href="/recipes/tags/old/">old</a>). </p> <p>This is an old program experimenting with CGI techniques in Python using <code>CGIHTTPServer</code> and designed for creating calendars. This is committed for archival to be run under Python 2.5 or later versions.</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> Yet Another Ordered Dictionary (Python) 2011-08-07T12:14:56-07:00Lucio Santihttp://code.activestate.com/recipes/users/4178886/http://code.activestate.com/recipes/577826-yet-another-ordered-dictionary/ <p style="color: grey"> Python recipe 577826 by <a href="/recipes/users/4178886/">Lucio Santi</a> (<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/linked_list/">linked_list</a>, <a href="/recipes/tags/ordered/">ordered</a>). </p> <p>An implementation of dictionaries preserving key insertion order. Despite using a doubly-linked list to keep track of the appropriate order, this list is actually embedded in the dictionary. As a consequence, there is little space penalty, and also every operation exhibits an efficient implementation (i.e., no need to perform lookups or deletions multiple times, as it happens with other versions of this data structure).</p> Trace decorator for debugging (Python) 2011-01-24T18:40:51-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577551-trace-decorator-for-debugging/ <p style="color: grey"> Python recipe 577551 by <a href="/recipes/users/4173535/">Kevin L. Sitze</a> (<a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/debug/">debug</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/inspection/">inspection</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/trace/">trace</a>). Revision 2. </p> <p>This package provides a decorator for tracing function and method calls in your applications. The tracing capabilities are managed through the logging package, and several mechanisms are provided for controlling the destination of the trace output.</p> <p>It also provides functionality for adding decorators to existing classes or modules.</p> PluginManager - Extending Project Functionality By Using Custom Modules/Plugins On The Fly. (Python) 2010-05-07T15:32:10-07:00AJ. Mayorgahttp://code.activestate.com/recipes/users/4173476/http://code.activestate.com/recipes/577216-pluginmanager-extending-project-functionality-by-u/ <p style="color: grey"> Python recipe 577216 by <a href="/recipes/users/4173476/">AJ. Mayorga</a> (<a href="/recipes/tags/extending/">extending</a>, <a href="/recipes/tags/extensible/">extensible</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/plugin/">plugin</a>). Revision 4. </p> <p>This demo shows how you can create and manage your own custom plugins for extending functionality in your Python projects. There are no safety wrappers in this demo for restricting plugins aside from that fact that plugins are run as an extention of a management class which is run in its own instance only receiving data passed to it by the RumModules method, that said security should ideally be applied to the ModuleAPI class, by restricting __builtins__ on eval calls and/or validating plugin content and parameters which can be done by extending the Prepaser Class. For a great recipe/demo of restricting eval call see <a href="http://code.activestate.com/recipes/496746" rel="nofollow">http://code.activestate.com/recipes/496746</a>. </p> <p>That aside it was alot of fun to write and use. Enjoy</p> <p>PS: you will need <a href="http://code.activestate.com/recipes/577144/" rel="nofollow">http://code.activestate.com/recipes/577144/</a> to import Xceptions</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> 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> CallPipe: Call the methods of an object in a remote process (Python) 2008-09-17T17:33:36-07:00david decotignyhttp://code.activestate.com/recipes/users/4129454/http://code.activestate.com/recipes/576509-callpipe-call-the-methods-of-an-object-in-a-remote/ <p style="color: grey"> Python recipe 576509 by <a href="/recipes/users/4129454/">david decotigny</a> (<a href="/recipes/tags/call/">call</a>, <a href="/recipes/tags/multiprocessing/">multiprocessing</a>, <a href="/recipes/tags/pipe/">pipe</a>, <a href="/recipes/tags/processing/">processing</a>, <a href="/recipes/tags/remote/">remote</a>). Revision 6. </p> <p>I am process A and I own object Obj. Now I decide to fork(): process B is born.</p> <p>How do I do to make process B call the methods of A's object Obj (and not the methods of its own copy of Obj...) ?</p> <p>This is what this module does: you create the "CallPipe" for Obj prior to fork()ing, and then process B can call any method of A's object Obj through it.</p> Threaded Documenting XML RPC server over HTTPS (Python) 2008-06-06T16:59:23-07:00Andrew Yingerhttp://code.activestate.com/recipes/users/4155123/http://code.activestate.com/recipes/573444-threaded-documenting-xml-rpc-server-over-https/ <p style="color: grey"> Python recipe 573444 by <a href="/recipes/users/4155123/">Andrew Yinger</a> (<a href="/recipes/tags/web/">web</a>). Revision 4. </p> <p>Server that runs a threaded, documenting XMLRCP server that uses HTTPS for transporting XML data.</p> <p>(This code borrow's heavily from Laszlo Nagy's: <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496786%29" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496786)</a></p> Easy testing of CGI scripts (Python) 2016-11-01T02:27:13-07:00Bryan Olsonhttp://code.activestate.com/recipes/users/1998252/http://code.activestate.com/recipes/550822-easy-testing-of-cgi-scripts/ <p style="color: grey"> Python recipe 550822 by <a href="/recipes/users/1998252/">Bryan Olson</a> (<a href="/recipes/tags/cgi/">cgi</a>). Revision 3. </p> <p>Invoking 'runcgi.py target.cgi' starts a built-in web server listening on the localhost adapter, configures the server to run target.cgi as a CGI program, then launches the system's default web browser to get the URL of the target script. The CGI script need not be in Python. The terminal that launched runcgi.py will show the server's log, which reports requests, responses, and errors.</p> Submit multiple concurrent batch jobs, with flexible throttling and timing. (Python) 2007-11-11T10:33:36-08:00Mark Pettithttp://code.activestate.com/recipes/users/4099718/http://code.activestate.com/recipes/534160-submit-multiple-concurrent-batch-jobs-with-flexibl/ <p style="color: grey"> Python recipe 534160 by <a href="/recipes/users/4099718/">Mark Pettit</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>Submit a series of batch commands (as expected by the os.system() function). Each will be submitted in a separate python thread, but will be a separate process, hence will take advantage of multi-core cpu's. A maximum number of simultaneous jobs can be set - throttling. A maximum time can be set before the routine will return, else it waits until all are completed.</p> Paint 2.0 (Python) 2007-03-28T15:30:34-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/511436-paint-20/ <p style="color: grey"> Python recipe 511436 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/graphics/">graphics</a>). </p> <p>This program was adapted from the SAMPLES directory that comes with Python. It allows two people to draw on a program with someone else watching and, at the same time watch what the other person is drawing also.</p> Paint 1.0 (Python) 2007-03-28T15:21:48-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/511434-paint-10/ <p style="color: grey"> Python recipe 511434 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/graphics/">graphics</a>). </p> <p>This program was adapted from the SAMPLES directory that comes with Python. It allows two people to draw on a program with someone else watching and, at the same time watch what the other person is drawing also.</p> spots (V2) (Python) 2007-03-28T15:28:58-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/511435-spots-v2/ <p style="color: grey"> Python recipe 511435 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>This recipe is the core of something called FDR (Full Duplex RPC). FDR has not been completed at this time, but the "spots" module takes most of the work off of other modules in network chatter. This is the second version of "spots" and simplifies the ZSP class while getting rid of the base255 module.</p> Restricted "safe" eval (Python) 2006-05-29T20:08:35-07:00Babar K. Zafarhttp://code.activestate.com/recipes/users/2904019/http://code.activestate.com/recipes/496746-restricted-safe-eval/ <p style="color: grey"> Python recipe 496746 by <a href="/recipes/users/2904019/">Babar K. Zafar</a> . Revision 3. </p> <p>Provides a retricted enviroment for dynamically executing a subset of the Python language suitable for configuration scripts, end-user oriented scripting and storing data in a user-friendly way.</p> spots (V1) (Python) 2007-03-28T15:17:45-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/511433-spots-v1/ <p style="color: grey"> Python recipe 511433 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>This program demonstrates some sample usage of the base255 module and is the core of something called FDR (Full Duplex RPC). FDR has not been completed at this time, but the "spots" module takes most of the work off of other modules in network chatter.</p> Module For Running Simple Proxies (Python) 2007-02-01T21:51:53-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/502204-module-for-running-simple-proxies/ <p style="color: grey"> Python recipe 502204 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>Proxies can be useful at times but may not be simple to create and run. This recipe provides a single class that can build proxy objects capable of being both started and stopped.</p> Persistent Queue (Python) 2007-06-27T11:43:13-07:00Kjetil Jacobsenhttp://code.activestate.com/recipes/users/2518557/http://code.activestate.com/recipes/501154-persistent-queue/ <p style="color: grey"> Python recipe 501154 by <a href="/recipes/users/2518557/">Kjetil Jacobsen</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). Revision 2. </p> <p>A class for persistent queues.</p>