Package park :: Package util :: Module threads :: Class AfterThread

Class AfterThread

source code


Thread class with additional 'after' capability which runs a function after the thread is complete. This allows us to separate the notification from the computation.

Unlike Thread.join, the wait() method returns the value of the computation.

Instance Methods
 
__init__(self, *args, **kwargs)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
after(self, notify=None)
Calls notify after the thread is complete.
source code
 
run(self)
Run the thread followed by the after function if any.
source code
 
wait(self, timeout=None)
Wait for the thread to complete.
source code

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

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

Properties
  name
Thread name

Inherited from object: __class__

Method Details

__init__(self, *args, **kwargs)
(Constructor)

source code 
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Overrides: object.__init__
(inherited documentation)

after(self, notify=None)

source code 
Calls notify after the thread is complete. Notify should take a single argument which is the result of the function.

run(self)

source code 
Run the thread followed by the after function if any.
Overrides: threading.Thread.run

wait(self, timeout=None)

source code 

Wait for the thread to complete.

Returns the result of the computation.

Example:

result = thread.wait()

If timeout is used, then wait() may return before the result is available. In this case, wait() will return None. This can be used as follows:

while True:
    result = thread.wait(timeout=0)
    if result is not None: break
    ... do something else while waiting ...

Timeout should not be used with functions that may return None. This is due to the race condition in which the thread completes between the timeout triggering in wait() and the main thread calling thread.isAlive().


Property Details

name

Thread name
Get Method:
unreachable.getName(self)
Set Method:
unreachable.setName(self, name)