Package mrv :: Module thread :: Class WorkerThread
[hide private]
[frames] | no frames]

Class WorkerThread

source code

        object --+            
                 |            
threading._Verbose --+        
                     |        
      threading.Thread --+    
                         |    
        TerminatableThread --+
                             |
                            WorkerThread

This base allows to call functions on class instances natively and retrieve their results asynchronously using a queue. The thread runs forever unless it receives the terminate signal using its task queue.

Tasks could be anything, but should usually be class methods and arguments to allow the following:

inq = Queue() outq = Queue() w = WorkerThread(inq, outq) w.start() inq.put((WorkerThread.<method>, args, kwargs)) res = outq.get()

finally we call quit to terminate asap.

alternatively, you can make a call more intuitively - the output is the output queue allowing you to get the result right away or later w.call(arg, kwarg='value').get()

inq.put(WorkerThread.quit) w.join()

You may provide the following tuples as task: t[0] = class method, function or instance method t[1] = optional, tuple or list of arguments to pass to the routine t[2] = optional, dictionary of keyword arguments to pass to the routine

Nested Classes [hide private]
  InvalidRoutineError
Class sent as return value in case of an error
Instance Methods [hide private]
 
__init__(self, inq=None, outq=None)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
call(self, function, *args, **kwargs)
Method that makes the call to the worker using the input queue, returning our output queue
source code
 
wait_until_idle(self)
wait until the input queue is empty, in the meanwhile, take all results off the output queue.
source code
 
run(self)
Process input tasks until we receive the quit signal
source code
 
quit(self) source code

Inherited from threading.Thread: __repr__, getName, isAlive, isDaemon, is_alive, join, setDaemon, setName

Inherited from threading.Thread (private): _set_daemon, _set_ident

Inherited from threading._Verbose (private): _note

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__

    Subclass Interface

Inherited from TerminatableThread: start

Inherited from TerminatableThread (private): _should_terminate, _terminated

    Interface
 
stop_and_join(self)
Send the stop signal to terminate, then join
source code

Inherited from TerminatableThread: schedule_termination

Properties [hide private]
  inq
  outq

Inherited from TerminatableThread (private): _terminate

Inherited from threading.Thread: daemon, ident, name

Inherited from object: __class__

Method Details [hide private]

__init__(self, inq=None, outq=None)
(Constructor)

source code 

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Overrides: object.__init__
(inherited documentation)

call(self, function, *args, **kwargs)

source code 
Method that makes the call to the worker using the input queue, returning our output queue
Parameters:
  • funciton - can be a standalone function unrelated to this class, a class method of this class or any instance method. If it is a string, it will be considered a function residing on this instance
  • args - arguments to pass to function

stop_and_join(self)

source code 
Send the stop signal to terminate, then join
Overrides: TerminatableThread.stop_and_join

run(self)

source code 
Process input tasks until we receive the quit signal
Overrides: threading.Thread.run