Module with threading utilities
Epydoc: mrv.thread.TerminatableThread
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
Epydoc: mrv.thread.WorkerThread
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
Bases: exceptions.Exception
Class sent as return value in case of an error
Method that makes the call to the worker using the input queue, returning our output queue
Parameters: |
|
---|---|
Parma **kwargs**kwargs: | |
kwargs to pass to function |