pdk.ThreadingClasses ($Date: 2002/12/04 10:13:05 $)
index
pdk/ThreadingClasses.py

Threading classes for pdk.

The Thread class provides more options in what can be passed as a target function, has a built-in progress report mechanism, and allows querying the result of the target function.

The ThreadedProcessor class offers interruptible threaded batch processing with arbitrary callables (e.g., for threaded processing of a large number of images with the same algorithm).

FOG 04.2001

 
Modules
            
Queue
inspect
threading
time
types
 
Classes
            
Queue.Queue
ClearableQueue
threading.Thread(threading._Verbose)
Thread
ThreadedProcessor
WorkerThreadPool
 
class ClearableQueue(Queue.Queue)
     
Purpose:adds a thread-safe .clear method to the standard Queue.Queue

 
   Methods defined here:
clear(self)
 
class Thread(threading.Thread)
     
Purpose:a sophisticated thread for pdk
Detail:features a) a mechanism to report a thread`s progress (if the target function defines an "update" keyword argument, it is passed a function that can be passed arbitrary data reflecting the thread`s state); b) the possibility to specify instance methods and class instances with a __call__ method as target functions; and c) a result value that can be queried with the .getResult method.

 
  
Method resolution order:
Thread
threading.Thread
threading._Verbose

Methods defined here:
__init__(self, group=None, target=None, name=None, args=(), options={})

For parameters, see also the standard library documentation for the threading.Thread base class.

Parameters:
  • group : should be None (reserved for future use)
  • target : the client function or method
  • name : a unique name for this thread; defaults to "Thread-N", with N being the current thread number
  • args : positional arguments for the target function
  • options : keyword arguments for the target function
getResult(self)
returns None until the .run method terminates, then returns the return value of the target function.
getUpdate(self)
if the target function was set up to call .setUpdate to report the thread`s progress, call this function from the main thread to get this progress information (e.g., to update a GUI indicator or some such...).
run(self)
run the associated thread target function with the current arguments.
setArgs(self, argT)
use this to change the positional arguments for the thread target function between subsequent calls to .run.
setOptions(self, optionD)
use this to change the keyword arguments for the thread target function between subsequent calls to .run.
setUpdate(self, updateData)
if the target function for the thread accepts an "update" argument, it will get passed to this method. Call it in your thread whenever the thread`s state changes.
 
class ThreadedProcessor
     
Purpose:threaded processing of repetitive tasks
Detail:uses a WorkerThreadPool to do the hard work. This class is useful whenever you have to call the same routine with the same arguments on a large number of items and want to either distribute the work across several processors, or be able to interrupt the processing, or both. Note that you have to set up a loop in your main program that calls .poll until it returns a non-None value.

 
   Methods defined here:
__call__(self, itemL, args=(), options={})
queues all items in the sequence`itemL` for threaded processing. Each element in itemL is going to be the first argument for each call to the callback specified in the constructor, followed by additional positional arguments given in args and keyword arguments given in options. An exception to this rules are tuples as elements in itemL; these are prepended to the list of positional arguments.
__init__(self, targetF, nThreads=None)
Parameters:
  • targetF: the routine to be called repetitively
  • nThreads: number of worker threads to use
poll(self)
returns None if
  1. there are still pending items to be processed, or
  2. the processor is not running

If neither of these conditions is met, a list containing the results for each item in the same order they were passed to .__call__ is returned.

stop(self)
stops the threaded processing of queued items.
 
class WorkerThreadPool
     
Purpose:a worker thread pool for algorithm processing of any kind
Detail:based on ideas presented in a tutorial by Aahz Maruch at IPC9. See http://starship.python.net/crew/aahz/IPC9/ThreadPoolSpider.py

 
   Methods defined here:
__init__(self, nThreads, sleepTime=None)
initializes nThreads worker threads in this pool (calls .reset).
feed(self, key, callback, args=(), options={})
queues the callable callback with positional arguments args and keyword arguments options for threaded processing. key is an integer that is used to identify the result returned by .poll.
poll(self)
returns None or the most current result from the result queue, which will be a 2-tuple (<key>,<return value>) consisting of the key and the result of the call defined by the arguments to a previous call to .feed. Note that results are by no means returned in the same order as they are fed into the queue (which is why we keep the key around...).
reset(self)
calls first .stop and then .restart.
restart(self)
restarts the worker threads.
stop(self)
stops all worker threads and clears input and output queues.
 
Data
             __author__ = '$Author: gathmann $'
__date__ = '$Date: 2002/12/04 10:13:05 $'
__revision__ = '$Revision: 1.9 $'
__source__ = '$Source: /home/cvs/pdk/pdk/ThreadingClasses.py,v $'
 
Author
            
$Author: gathmann $