mrv.thread

Epydoc: mrv.thread

Module with threading utilities

Functions

mrv.thread.do_terminate_threads(whitelist=[])
Simple function which terminates all of our threads :param whitelist: If whitelist is given, only the given threads will be terminated
mrv.thread.terminate_threads(func)
Kills all worker threads the method has created by sending the quit signal. This takes over in case of an error in the main function

Classes

Epydoc: mrv.thread.TerminatableThread

class mrv.thread.TerminatableThread(*args, **kwargs)

Bases: threading.Thread

A simple thread able to terminate itself on behalf of the user.

Terminate a thread as follows:

t.stop_and_join()

Derived classes call _should_terminate() to determine whether they should abort gracefully

daemon
getName()
ident
isAlive()
isDaemon()
is_alive()
join(timeout=None)
name
run()
schedule_termination()
Schedule this thread to be terminated as soon as possible. :note: this method does not block.
setDaemon(daemonic)
setName(name)
start()
Start the thread and return self
stop_and_join()
Ask the thread to stop its operation and wait for it to terminate :note: Depending on the implenetation, this might block a moment

Epydoc: mrv.thread.WorkerThread

class mrv.thread.WorkerThread(inq=None, outq=None)

Bases: mrv.thread.TerminatableThread

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

exception InvalidRoutineError

Bases: exceptions.Exception

Class sent as return value in case of an error

args
message
WorkerThread.call(function, *args, **kwargs)

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
Parma **kwargs**kwargs:
 

kwargs to pass to function

WorkerThread.daemon
WorkerThread.getName()
WorkerThread.ident
WorkerThread.inq
WorkerThread.isAlive()
WorkerThread.isDaemon()
WorkerThread.is_alive()
WorkerThread.join(timeout=None)
WorkerThread.name
WorkerThread.outq
WorkerThread.quit()
WorkerThread.run()
Process input tasks until we receive the quit signal
WorkerThread.schedule_termination()
Schedule this thread to be terminated as soon as possible. :note: this method does not block.
WorkerThread.setDaemon(daemonic)
WorkerThread.setName(name)
WorkerThread.start()
Start the thread and return self
WorkerThread.stop_and_join()
Send the stop signal to terminate, then join
WorkerThread.wait_until_idle()
wait until the input queue is empty, in the meanwhile, take all results off the output queue.

Table Of Contents

Previous topic

mrv.dge

Next topic

mrv.info

This Page