Popular recipes by david decotigny http://code.activestate.com/recipes/users/4129454/2016-01-30T00:40:20-08:00ActiveState Code RecipesThread 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 <write your guess here>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>
Track new/unreclaimed objects between 2 points in the code (Python)
2008-10-03T12:14:52-07:00david decotignyhttp://code.activestate.com/recipes/users/4129454/http://code.activestate.com/recipes/576523-track-newunreclaimed-objects-between-2-points-in-t/
<p style="color: grey">
Python
recipe 576523
by <a href="/recipes/users/4129454/">david decotigny</a>
(<a href="/recipes/tags/cycle/">cycle</a>, <a href="/recipes/tags/garbage/">garbage</a>, <a href="/recipes/tags/leak/">leak</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/reclaim/">reclaim</a>).
Revision 3.
</p>
<p>This module provides 3 ways of detecting which objects have been allocated (methods 1 and 3) or became un-reclaimable (method 2) between 2 points in the code. It can be very useful to detect memory leaks (eg. cycles involving objects with a __del__ method).</p>
Convert datetime.datetime objects to/from Boost.python posix time (Python)
2008-09-17T23:55:23-07:00david decotignyhttp://code.activestate.com/recipes/users/4129454/http://code.activestate.com/recipes/576395-convert-datetimedatetime-objects-tofrom-boostpytho/
<p style="color: grey">
Python
recipe 576395
by <a href="/recipes/users/4129454/">david decotigny</a>
(<a href="/recipes/tags/boost/">boost</a>).
Revision 10.
</p>
<p>This recipe allows to transparently convert python's datetime.datetime objects to and from boost's boost::posix_time::ptime objects.</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>
processing.Pool variation which allows multiple threads to send the same requests without incurring duplicate processing (Python)
2008-09-17T17:01:21-07:00david decotignyhttp://code.activestate.com/recipes/users/4129454/http://code.activestate.com/recipes/576462-processingpool-variation-which-allows-multiple-thr/
<p style="color: grey">
Python
recipe 576462
by <a href="/recipes/users/4129454/">david decotigny</a>
(<a href="/recipes/tags/map/">map</a>, <a href="/recipes/tags/parallel/">parallel</a>, <a href="/recipes/tags/pool/">pool</a>, <a href="/recipes/tags/processing/">processing</a>, <a href="/recipes/tags/threads/">threads</a>).
Revision 3.
</p>
<p>processing.Pool (<a href="http://pypi.python.org/pypi/processing" rel="nofollow">http://pypi.python.org/pypi/processing</a>) is a nice tool to "parallelize" map() on multiple CPUs.
However, imagine you have X threads which send the same request Pool.map(getNthPrimeNumber, [100000, 10000000, 10000]) at (almost) the same time. Obviously, you don't want to compute X times getNthPrimeNumber for 100000, 10000000, 10000... unless you have 3.X processors available. You would like one thread to submit the 3 requests, and then the X-1 others would notice that the requests have already been submitted and will then just wait for the result.
This is what this code is about: a kind of "trensient memoize" for processing.Pool::imap().</p>
JSON RPC Server and client (Python)
2008-04-01T09:50:49-07:00david decotignyhttp://code.activestate.com/recipes/users/4129454/http://code.activestate.com/recipes/552751-json-rpc-server-and-client/
<p style="color: grey">
Python
recipe 552751
by <a href="/recipes/users/4129454/">david decotigny</a>
(<a href="/recipes/tags/network/">network</a>).
</p>
<p>This recipe shows how to create JSON RPC client and server objects. The aim is to mimic the standard python XML-RPC API both on the client and server sides, but using JSON marshalling. It depends on cjson (<a href="http://pypi.python.org/pypi/python-cjson" rel="nofollow">http://pypi.python.org/pypi/python-cjson</a>) for the encoding/decoding of JSON data. This recipe tries to reuse the code of XML-RPC as much as possible.</p>
Create a restricted python function from a string (Python)
2008-08-05T22:15:18-07:00david decotignyhttp://code.activestate.com/recipes/users/4129454/http://code.activestate.com/recipes/550804-create-a-restricted-python-function-from-a-string/
<p style="color: grey">
Python
recipe 550804
by <a href="/recipes/users/4129454/">david decotigny</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 2.
</p>
<p>The createFunction(sourceCode) below returns a python function that executes the given sourceCode (a string containing python code). The function, being a real python function, doesn't incur any overhead compared to any normal python function. And its environment is controlled: by default only safe operations are permitted (ie. map, reduce, filter, list, etc. ; others like import, open, close, eval, etc. are forbidden by default). But it is possible to extend this environment.</p>