Top-rated recipes tagged "threads"http://code.activestate.com/recipes/tags/threads/top/2012-03-26T14:56:17-07:00ActiveState Code RecipesCreating a daemon the Python way (Python)
2005-10-03T16:49:09-07:00Chad J. Schroederhttp://code.activestate.com/recipes/users/1760491/http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/
<p style="color: grey">
Python
recipe 278731
by <a href="/recipes/users/1760491/">Chad J. Schroeder</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 6.
</p>
<p>The Python way to detach a process from the controlling terminal and run it in the
background as a daemon.</p>
Fork a daemon process on Unix (Python)
2001-07-10T14:01:38-07:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/66012-fork-a-daemon-process-on-unix/
<p style="color: grey">
Python
recipe 66012
by <a href="/recipes/users/98061/">Jürgen Hermann</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>Forking a daemon on Unix requires a certain sequence of system calls. Since Python exposes a full POSIX interface, this can be done in Python, too.</p>
Generator Attributes (Python)
2002-12-09T08:44:56-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/164044-generator-attributes/
<p style="color: grey">
Python
recipe 164044
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 3.
</p>
<p>Function to enable attribute access for generator instances. Simplifies data sharing for advanced uses of generators and provides much of the functionality sought by PEP 288.
<br>
Most uses of generators have no need for data sharing. This recipe is for the few tough cases which can be written more elegantly when attribute access is enabled.</p>
Easy threading with Futures (Python)
2002-04-16T21:12:21-07:00David Perryhttp://code.activestate.com/recipes/users/138465/http://code.activestate.com/recipes/84317-easy-threading-with-futures/
<p style="color: grey">
Python
recipe 84317
by <a href="/recipes/users/138465/">David Perry</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 2.
</p>
<p>Although Python's thread syntax is nicer than in many languages, it can still be a pain if all one wants to do is run a time-consuming function in a separate thread, while allowing the main thread to continue uninterrupted. A Future provides a legible and intuitive way to achieve such an end.</p>
Sleepsort (Python)
2011-06-17T05:17:22-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577756-sleepsort/
<p style="color: grey">
Python
recipe 577756
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/sorting/">sorting</a>, <a href="/recipes/tags/threads/">threads</a>).
Revision 2.
</p>
<p>Funky sort routine to demonstrate Python's threading basics.</p>
Thread pool mixin class for use with SocketServer.TCPServer (Python)
2008-07-10T18:52:58-07:00Michael Palmerhttp://code.activestate.com/recipes/users/1827292/http://code.activestate.com/recipes/574454-thread-pool-mixin-class-for-use-with-socketservert/
<p style="color: grey">
Python
recipe 574454
by <a href="/recipes/users/1827292/">Michael Palmer</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 2.
</p>
<p>This is intended as a drop-in replacement for the ThreadingMixIn class in module SocketServer of the standard lib. Instead of spawning a new thread for each request, requests are processed by of pool of reusable threads.</p>
Run async code inline, nonblocking (Python)
2009-11-11T12:55:01-08:00Thomas Ahlehttp://code.activestate.com/recipes/users/4060075/http://code.activestate.com/recipes/576952-run-async-code-inline-nonblocking/
<p style="color: grey">
Python
recipe 576952
by <a href="/recipes/users/4060075/">Thomas Ahle</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>).
Revision 7.
</p>
<p>A decorator, that makes it easy to switch between the mainthread and background threads.</p>
TaskQueue (Python)
2006-03-25T13:00:38-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/475160-taskqueue/
<p style="color: grey">
Python
recipe 475160
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 3.
</p>
<p>Queue subclass to simplify working with consumer threads. Keeps a count of tasks put into the queue and lets the consumer thread report when each task has been retrieved AND PROCESSED COMPLETELY. This reporting supports a join() method that blocks untils all submitted tasks have been fully processed.</p>
Basic synchronization decorator (Python)
2005-12-19T22:29:46-08:00John Fouhyhttp://code.activestate.com/recipes/users/2466667/http://code.activestate.com/recipes/465057-basic-synchronization-decorator/
<p style="color: grey">
Python
recipe 465057
by <a href="/recipes/users/2466667/">John Fouhy</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>Synchronization has got to be one of the most popular uses for decorators, but there's no recipe here. So, this is a simple synchronization decorator.</p>
Decorator to make a decorated call in a separate thread with timeout (Python)
2005-10-31T05:15:57-08:00Dmitry Dvoinikovhttp://code.activestate.com/recipes/users/2475216/http://code.activestate.com/recipes/440569-decorator-to-make-a-decorated-call-in-a-separate-t/
<p style="color: grey">
Python
recipe 440569
by <a href="/recipes/users/2475216/">Dmitry Dvoinikov</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 8.
</p>
<p>Makes it easier to execute async calls or deal with external systems calls to which can block forever or occassionally take long time to complete.</p>
A generic programming thread pool (Python)
2003-09-24T17:24:43-07:00Tim Lesherhttp://code.activestate.com/recipes/users/161138/http://code.activestate.com/recipes/203871-a-generic-programming-thread-pool/
<p style="color: grey">
Python
recipe 203871
by <a href="/recipes/users/161138/">Tim Lesher</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 3.
</p>
<p>A thread pool class that takes arbitrary callables as work units, and supports callbacks when the work unit is complete.</p>
Threads, Tkinter and asynchronous I/O (Python)
2001-10-21T07:14:35-07:00Jacob Hallénhttp://code.activestate.com/recipes/users/136936/http://code.activestate.com/recipes/82965-threads-tkinter-and-asynchronous-io/
<p style="color: grey">
Python
recipe 82965
by <a href="/recipes/users/136936/">Jacob Hallén</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>This recipe shows the easiest way of handling access to sockets, serial ports
and other asynchronous I/O ports while running a Tkinter based GUI.
It allows for a worker thread to block in a select(). Whenever something arrives
it will received and inserted in a queue. The main (GUI) thread then polls
the queue 10 times per second (often enough so the user will not notice any
significant delay), and processes all messages that have arrived.</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>
Spin Cursor (Python)
2007-10-31T13:22:40-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/534142-spin-cursor/
<p style="color: grey">
Python
recipe 534142
by <a href="/recipes/users/760763/">Anand</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>A class providing a threaded spin cursor on the console.</p>
Multiple-reader-one-writer (MROW) resource locking (Python)
2005-05-07T04:23:52-07:00Matthew Scotthttp://code.activestate.com/recipes/users/2422085/http://code.activestate.com/recipes/413393-multiple-reader-one-writer-mrow-resource-locking/
<p style="color: grey">
Python
recipe 413393
by <a href="/recipes/users/2422085/">Matthew Scott</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 2.
</p>
<p>In multithreaded apps, there is at times the need to control access to a resource to ensure data consistency and integrity. Multiple-reader, one-writer locking allows efficient read access by multiple threads, while ensuring that a write does not overlap any reads nor another write.</p>
Thread Control Idiom (Python)
2001-07-05T06:31:54-07:00Doug Forthttp://code.activestate.com/recipes/users/118211/http://code.activestate.com/recipes/65448-thread-control-idiom/
<p style="color: grey">
Python
recipe 65448
by <a href="/recipes/users/118211/">Doug Fort</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>This is a basic idiom I use on almost every thread I write.</p>
Run a task every few seconds (Python)
2001-06-18T04:31:57-07:00Itamar Shtull-Trauringhttp://code.activestate.com/recipes/users/98053/http://code.activestate.com/recipes/65222-run-a-task-every-few-seconds/
<p style="color: grey">
Python
recipe 65222
by <a href="/recipes/users/98053/">Itamar Shtull-Trauring</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>The TaskThread class allows you to create threads that execute a specific action at a specified interval, by subclassing - just override the task() method. You can also shutdown a TaskThread without having to wait for it to finish "sleeping" (due to the use of threading.Event objects as an alternative to time.sleep()).</p>
Generic way to create a daemonized process in python (Python)
2012-03-26T14:56:17-07:00ajaymenon.khttp://code.activestate.com/recipes/users/4181225/http://code.activestate.com/recipes/578072-generic-way-to-create-a-daemonized-process-in-pyth/
<p style="color: grey">
Python
recipe 578072
by <a href="/recipes/users/4181225/">ajaymenon.k</a>
(<a href="/recipes/tags/daemon/">daemon</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/threads/">threads</a>).
Revision 4.
</p>
<p>A simple daemon that will constantly keep track the size of your inbox and when it exceeds a certain size, will send you a reminder email.</p>
A multithreaded, concurrent version of map() (Python)
2010-08-16T23:04:48-07:00Wai Yip Tunghttp://code.activestate.com/recipes/users/2382677/http://code.activestate.com/recipes/577360-a-multithreaded-concurrent-version-of-map/
<p style="color: grey">
Python
recipe 577360
by <a href="/recipes/users/2382677/">Wai Yip Tung</a>
(<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/multithreading/">multithreading</a>, <a href="/recipes/tags/threads/">threads</a>).
</p>
<p>map() applies a function to a list of data sequentially. This is a variation to map that execute each function call concurrently in a thread.</p>
Heise mp3 downloader (Python)
2009-01-17T11:51:39-08:00d.schlabinghttp://code.activestate.com/recipes/users/4168903/http://code.activestate.com/recipes/576618-heise-mp3-downloader/
<p style="color: grey">
Python
recipe 576618
by <a href="/recipes/users/4168903/">d.schlabing</a>
(<a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/script/">script</a>, <a href="/recipes/tags/threads/">threads</a>).
</p>
<p>This little script presents new heise-news-articles individually by title and asks if it should download the corresponding mp3-file.</p>