Popular Python recipes tagged "meta:requires=threading"http://code.activestate.com/recipes/langs/python/tags/meta:requires=threading/2015-06-06T12:13:00-07:00ActiveState Code RecipesTail multiple pidgin IRC logfiles (Python) 2015-06-06T12:13:00-07:00Anton Vredegoorhttp://code.activestate.com/recipes/users/2667360/http://code.activestate.com/recipes/579066-tail-multiple-pidgin-irc-logfiles/ <p style="color: grey"> Python recipe 579066 by <a href="/recipes/users/2667360/">Anton Vredegoor</a> (<a href="/recipes/tags/colorize/">colorize</a>, <a href="/recipes/tags/irc/">irc</a>, <a href="/recipes/tags/iterators/">iterators</a>, <a href="/recipes/tags/logfiles/">logfiles</a>, <a href="/recipes/tags/merging/">merging</a>, <a href="/recipes/tags/nonblocking/">nonblocking</a>, <a href="/recipes/tags/pidgin/">pidgin</a>, <a href="/recipes/tags/tail/">tail</a>, <a href="/recipes/tags/threads/">threads</a>). Revision 2. </p> <p>Tail multiple pidgin IRC logfiles. </p> <p>Pidgin should be connected to IRC with the channels one wants to tail joined, and it should save logs as text.</p> <p>The script needs two arguments:</p> <pre class="prettyprint"><code>the directory containing the directories with channel logs a list of channel names, quoted and separated by spaces </code></pre> <p>Example command:</p> <p>python pidgin-irctail.py -d <a href="mailto:~/.purple/logs/irc/YOUR_IRC_HANDLE@irc.freenode.net">~/.purple/logs/irc/YOUR_IRC_HANDLE@irc.freenode.net</a> -c "#chan1 #chan2 #chan3"</p> <p>Some text elements are higlighted, and channel names are inserted into the log lines after the time info.</p> <p>If more than one channel is entered, the output of the logs is merged. </p> Analog Clock (Python) 2014-05-14T20:53:39-07:00Paulo Cavalcantihttp://code.activestate.com/recipes/users/4189949/http://code.activestate.com/recipes/578875-analog-clock/ <p style="color: grey"> Python recipe 578875 by <a href="/recipes/users/4189949/">Paulo Cavalcanti</a> (<a href="/recipes/tags/clock/">clock</a>). Revision 3. </p> <p>A python-tkinter analog clock</p> Py2.6+ and Py3.0+ backport of Python 3.3's LRU Cache (Python) 2013-03-06T05:38:15-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/578078-py26-and-py30-backport-of-python-33s-lru-cache/ <p style="color: grey"> Python recipe 578078 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/lru/">lru</a>). Revision 6. </p> <p>Full-featured O(1) LRU cache backported from Python3.3. The full Py3.3 API is supported (thread safety, maxsize, keyword args, type checking, __wrapped__, and cache_info). Includes Py3.3 optimizations for better memory utilization, fewer dependencies, and fewer dict lookups.</p> Pyqt / 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> Python 3 Token Bucket (Rate Limit) (Python) 2013-09-16T23:41:46-07:00Esteban Castro Borsanihttp://code.activestate.com/recipes/users/4184010/http://code.activestate.com/recipes/578659-python-3-token-bucket-rate-limit/ <p style="color: grey"> Python recipe 578659 by <a href="/recipes/users/4184010/">Esteban Castro Borsani</a> (<a href="/recipes/tags/bucket/">bucket</a>, <a href="/recipes/tags/limit/">limit</a>, <a href="/recipes/tags/rate/">rate</a>, <a href="/recipes/tags/ratelimit/">ratelimit</a>, <a href="/recipes/tags/token/">token</a>, <a href="/recipes/tags/tokenbucket/">tokenbucket</a>). </p> <p>A thread safe implementation.</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> Generator with lookahead (Python) 2013-09-24T10:44:56-07:00Rutger Saalminkhttp://code.activestate.com/recipes/users/4187940/http://code.activestate.com/recipes/578671-generator-with-lookahead/ <p style="color: grey"> Python recipe 578671 by <a href="/recipes/users/4187940/">Rutger Saalmink</a> (<a href="/recipes/tags/background/">background</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/lookahead/">lookahead</a>, <a href="/recipes/tags/performance/">performance</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Python generators are a great way of reducing memory usage due to the lazy (on demand) generation of values. However, when the process-time of generating such a value is relatively high, we can improve performance even more by obtaining the next n values of the generator in a separate thread in the background. Hence, the BackgroundGenerator.</p> Threaded communication with subprocess (Python) 2013-03-13T06:06:16-07:00Jan Müllerhttp://code.activestate.com/recipes/users/4183984/http://code.activestate.com/recipes/578488-threaded-communication-with-subprocess/ <p style="color: grey"> Python recipe 578488 by <a href="/recipes/users/4183984/">Jan Müller</a> (<a href="/recipes/tags/async/">async</a>, <a href="/recipes/tags/ipc/">ipc</a>, <a href="/recipes/tags/messaging/">messaging</a>, <a href="/recipes/tags/multithreading/">multithreading</a>, <a href="/recipes/tags/pipe/">pipe</a>, <a href="/recipes/tags/queue/">queue</a>, <a href="/recipes/tags/subprocess/">subprocess</a>). Revision 3. </p> <p>This recipe shows how to domesticate another executable as a service in a subprocess.</p> download the Activestate cook book recipe (Python) 2013-01-29T16:24:22-08:00lwz7512http://code.activestate.com/recipes/users/4185066/http://code.activestate.com/recipes/578439-download-the-activestate-cook-book-recipe/ <p style="color: grey"> Python recipe 578439 by <a href="/recipes/users/4185066/">lwz7512</a> . </p> <p>Small effort to store the python recipes to our local</p> <p>similar effort by other people: 543267-i-will-download-all-of-the-recipes-from-the-python 535162-i-download-all-the-python-cookbook-recipes</p> Simple python thread manager (Python) 2012-12-17T16:19:12-08:00Tom Wissinghttp://code.activestate.com/recipes/users/4184629/http://code.activestate.com/recipes/578385-simple-python-thread-manager/ <p style="color: grey"> Python recipe 578385 by <a href="/recipes/users/4184629/">Tom Wissing</a> (<a href="/recipes/tags/manager/">manager</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/threads/">threads</a>). </p> <p>A simple python static class handling basic threading. Designed for "worker" threads that loop forever over a function and the like.</p> Python: A event/signal dispatcher (Python) 2013-07-29T05:34:24-07:00Esteban Castro Borsanihttp://code.activestate.com/recipes/users/4184010/http://code.activestate.com/recipes/578307-python-a-eventsignal-dispatcher/ <p style="color: grey"> Python recipe 578307 by <a href="/recipes/users/4184010/">Esteban Castro Borsani</a> (<a href="/recipes/tags/dispatcher/">dispatcher</a>, <a href="/recipes/tags/events/">events</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/signals/">signals</a>). </p> <p>This is a thread-safe dispatcher.</p> Simple python thread manager (Python) 2012-11-17T23:18:17-08:00Tino Deakhttp://code.activestate.com/recipes/users/4184300/http://code.activestate.com/recipes/578331-simple-python-thread-manager/ <p style="color: grey"> Python recipe 578331 by <a href="/recipes/users/4184300/">Tino Deak</a> (<a href="/recipes/tags/manager/">manager</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/threads/">threads</a>). </p> <p>A simple python static class handling basic threading. Designed for "worker" threads that loop forever over a function and the like.</p> DoubleDict (Python) 2012-07-24T21:24:14-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578224-doubledict/ <p style="color: grey"> Python recipe 578224 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/dictionary/">dictionary</a>). </p> <p>After seeing requests for being able to access keys in a dictionary by value, the following recipe was born. It creates the <code>DoubleDict</code> class and allows just that. To ensure that only one key is returned when accessing it by value, values must be unique just as keys are unique, and this rule is automatically enforced. Most dictionary methods are supported, and many more are added to allow working with the dictionary from the view of the values instead of the keys. Several optional metaclasses are also provided to enable optional features in the <code>DoubleDict</code> class such as data consistency checks and atomic method execution.</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> download the Activestate cook book recipe (Python) 2012-07-04T12:14:22-07:00Sudeep AMhttp://code.activestate.com/recipes/users/4182702/http://code.activestate.com/recipes/578193-download-the-activestate-cook-book-recipe/ <p style="color: grey"> Python recipe 578193 by <a href="/recipes/users/4182702/">Sudeep AM</a> . Revision 2. </p> <p>Small effort to store the python recipes to our local</p> <p>similar effort by other people: 543267-i-will-download-all-of-the-recipes-from-the-python 535162-i-download-all-the-python-cookbook-recipes</p> Reader-Writer lock with priority for writers (Python) 2011-09-28T21:45:04-07:00Mateusz Koboshttp://code.activestate.com/recipes/users/4178730/http://code.activestate.com/recipes/577803-reader-writer-lock-with-priority-for-writers/ <p style="color: grey"> Python recipe 577803 by <a href="/recipes/users/4178730/">Mateusz Kobos</a> (<a href="/recipes/tags/locking/">locking</a>, <a href="/recipes/tags/threads/">threads</a>). Revision 2. </p> <p>The following class implements a reader-writer lock to use in the second readers-writers problem with python threads. In this problem, many readers can simultaneously access a share, and a writer has an exclusive access to this share. Additionally, the following constraints should be met: 1) no reader should be kept waiting if the share is currently opened for reading unless a writer is also waiting for the share, 2) no writer should be kept waiting for the share longer than absolutely necessary.</p> Observer pattern implemented with Descriptor class (Python) 2012-02-14T23:43:53-08:00Rodney Drenthhttp://code.activestate.com/recipes/users/4050661/http://code.activestate.com/recipes/578038-observer-pattern-implemented-with-descriptor-class/ <p style="color: grey"> Python recipe 578038 by <a href="/recipes/users/4050661/">Rodney Drenth</a> (<a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/observer/">observer</a>, <a href="/recipes/tags/publish/">publish</a>, <a href="/recipes/tags/reactive/">reactive</a>, <a href="/recipes/tags/subscribe/">subscribe</a>). </p> <p>The observer pattern is implemented using an observable descriptor. One creates instances of observable in a class, which allows observers to subscribe to changes in the observable. Assigning a value to the observable causes the suscribers to be notified. An observable can subscribe to another observable, in which case changes to the second propagate to subscribers of the first. The subscribe method returns a Subscription object. When this object is deleted or becomes unreferenced, the subscription is cancelled.</p> <p>This version compatible with Python 3.0 Example uses unittest to help in understanding the functionality.</p> Caching decorator with timeout, selective invalidation (Python) 2005-11-05T05:47:03-08:00Greg Steffensenhttp://code.activestate.com/recipes/users/2649990/http://code.activestate.com/recipes/442513-caching-decorator-with-timeout-selective-invalidat/ <p style="color: grey"> Python recipe 442513 by <a href="/recipes/users/2649990/">Greg Steffensen</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 5. </p> <p>A caching decorator that garbage collects in a separate thread (for performance), allows each cached function to (optionally) set a custom maximum age for entries, and allows individual cache entries to be selectively invalidated.</p> Run async code inline, nonblocking (Python) 2011-11-23T10:13:31-08:00PRITAM Khttp://code.activestate.com/recipes/users/4180048/http://code.activestate.com/recipes/577959-run-async-code-inline-nonblocking/ <p style="color: grey"> Python recipe 577959 by <a href="/recipes/users/4180048/">PRITAM K</a> (<a href="/recipes/tags/asynchronous/">asynchronous</a>, <a href="/recipes/tags/closure/">closure</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/threads/">threads</a>). </p> <p>A decorator, that makes it easy to switch between the mainthread and background threads.</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>