Most viewed recipes tagged "threads"http://code.activestate.com/recipes/tags/threads/views/2012-08-09T20:14:33-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>
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>
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>
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>
Timeout function using threading (Python)
2006-02-09T14:30:29-08:00dustin leehttp://code.activestate.com/recipes/users/2771318/http://code.activestate.com/recipes/473878-timeout-function-using-threading/
<p style="color: grey">
Python
recipe 473878
by <a href="/recipes/users/2771318/">dustin lee</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>Using signals to timeout a function such as in:
<a href="http://www.pycs.net/users/0000231/weblog/2004/10/" rel="nofollow">http://www.pycs.net/users/0000231/weblog/2004/10/</a>
<a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/307871" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/307871</a>
won't work if the function you are calling overrides the alarm. Using threads gives you away around this.</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>
Capturing the output and error streams from a unix shell command (Python)
2001-06-22T23:22:21-07:00Brent Burleyhttp://code.activestate.com/recipes/users/98036/http://code.activestate.com/recipes/52296-capturing-the-output-and-error-streams-from-a-unix/
<p style="color: grey">
Python
recipe 52296
by <a href="/recipes/users/98036/">Brent Burley</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 2.
</p>
<p>This recipe shows how to execute a unix shell command and capture the
output and error streams in python. By contrast, os.system() sends
both streams directly to the shell.</p>
Event Scheduling (threading.Timer) (Python)
2006-06-16T06:09:43-07:00James Kassemihttp://code.activestate.com/recipes/users/2916378/http://code.activestate.com/recipes/496800-event-scheduling-threadingtimer/
<p style="color: grey">
Python
recipe 496800
by <a href="/recipes/users/2916378/">James Kassemi</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>An accurate, simple event scheduler using blocking and event notification features available in the threading module.</p>
MultiThread support for SQLite access. (Python)
2010-07-20T14:29:35-07:00Louis RIVIEREhttp://code.activestate.com/recipes/users/4035877/http://code.activestate.com/recipes/526618-multithread-support-for-sqlite-access/
<p style="color: grey">
Python
recipe 526618
by <a href="/recipes/users/4035877/">Louis RIVIERE</a>
(<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/sqlite/">sqlite</a>, <a href="/recipes/tags/threads/">threads</a>).
Revision 4.
</p>
<p>Workaround for the SQLite limitation that prevents multiple threads from sharing a Connection object.</p>
thread2 -- killable threads (Python)
2012-08-09T20:14:33-07:00tomer filibahttp://code.activestate.com/recipes/users/2520014/http://code.activestate.com/recipes/496960-thread2-killable-threads/
<p style="color: grey">
Python
recipe 496960
by <a href="/recipes/users/2520014/">tomer filiba</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>A little module that extends the threading's module functionality -- allows one thread to raise exceptions in the context of another thread. By raising SystemExit, you can finally kill python threads :)</p>
Creating a single instance application (Python)
2006-02-22T06:37:05-08:00Dragan Jovelichttp://code.activestate.com/recipes/users/2787616/http://code.activestate.com/recipes/474070-creating-a-single-instance-application/
<p style="color: grey">
Python
recipe 474070
by <a href="/recipes/users/2787616/">Dragan Jovelic</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>Sometimes it is necessary to ensure that only one instance of application is running. This quite simple solution uses mutex to achieve this, and will run only on Windows platform.</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>
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>
CallQueue: Easy inter-thread communication (Python)
2006-04-22T08:52:08-07:00R Khttp://code.activestate.com/recipes/users/2810477/http://code.activestate.com/recipes/491281-callqueue-easy-inter-thread-communication/
<p style="color: grey">
Python
recipe 491281
by <a href="/recipes/users/2810477/">R K</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 2.
</p>
<p>A framework for easy (2-way) inter-thread communication resembling normal function calling.</p>
<p>Especially useful for non-blocking UI techniques and for load distribution on jerky resources. Can replace stiff Queue.Queue techniques in most cases - making threading code more readable and functional.</p>
<p>CallQueue lets you express function directly in local context, but execute things in a target thread. It focuses naturally on 2-way communication (with return value responses) and includes a fluid concept for inter-thread exception (transfer) issues. Supports also multi-producer, multi-consumer communication.</p>
<p>A target thread just has to do callqueue.receive() periodically without worrying about any data passing. Thus CallQueue also supports naturally a high-level bulk threading concept with anonymous "default consumer threads": Allocated "thread resources" can be thrown efficently on bunches of jobs.</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>
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>
profiling threads (Python)
2006-01-08T14:01:16-08:00Maciej Obarskihttp://code.activestate.com/recipes/users/2559120/http://code.activestate.com/recipes/465831-profiling-threads/
<p style="color: grey">
Python
recipe 465831
by <a href="/recipes/users/2559120/">Maciej Obarski</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 8.
</p>
<p>This recipe shows how to profile threads in Python by using custom profiler function.</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>
[Twisted] From blocking functions to deferred functions (Python)
2005-08-19T09:05:30-07:00Michele Simionatohttp://code.activestate.com/recipes/users/1122360/http://code.activestate.com/recipes/439358-twisted-from-blocking-functions-to-deferred-functi/
<p style="color: grey">
Python
recipe 439358
by <a href="/recipes/users/1122360/">Michele Simionato</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 2.
</p>
<p>Twisted FAQs clearly state that "deferreds do not magically convert blocking
code into non-blocking code". So, how do you magically convert blocking
code into non-blocking code?</p>
<p>This recipe is the solution!</p>