class eg.ThreadWorker(*args, **kwargs)

General purpose message pumping thread, that is used in many places.


Start the thread execution.

The thread will first call the self.Setup method with the parameters assigned by the eg.ThreadWorker constructor and waits till Setup() has finished its execution. If an exception is raised in self.Setup, this exception will be propagated to the self.Start method, so it will look like self.Start has directly called Setup(). But Setup() is actually executed in the new thread.


Call this if the thread should stop.

Setup(*args, **kwargs)

This will be called inside the thread at the beginning.

Any exception raised in this method will be propagated to the Start() method and re-raised there.


This will be called inside the thread when it finishes. It will even be called if the thread exits through an exception.

Call(func, *args, **kwargs)

Queue a function and its arguments for execution in the eg.ThreadWorker thread. Doesn’t wait for the completion of the function.

Func(func, timeout=None)

Wraps a function for synchronized execution in the eg.ThreadWorker thread.

The returned object can be called like the unwrapped function but will execute in the eg.ThreadWorker thread regardless were it is called from. The call will deliver the return value of the function. If the function raises an exception, it will be propagated to the calling thread.

The optional timeout value specifies the maximal time to wait for the completion of the function in floating point seconds. If the timeout expires, the call will raise an exception. If timeout is not specified or None (the default), it will wait forever.

Example usage:

threadWorker = eg.ThreadWorker()

def MyFunction(param):
    print param
    return "Hello Caller!"

wrappedFunc = threadWorker.Func(MyFunction, 5.0)
result = wrappedFunc("Hello World!")
print result
CallWait(func, timeout=10.0)

Queue a function for execution in the eg.ThreadWorker thread. Waits for completion of the function and returns its result.

The function must have no arguments, so if some are needed one should create a argumentless function with functools.partial.


This function is deprecated. Use the Func() wrapper instead.

Previous topic


Next topic


This Page