#!/usr/bin/env python

import threading
from time import time, ctime

class MyThread(threading.Thread):
    '''
    MyThread derives from the threading.Thread baseclass.
    Rather than a callable class, the run() method is
    automatically invoked when the thread begins execution.
    '''
    # constructor
    def __init__(self, func, args, name=''):
        threading.Thread.__init__(self)
        self.name = name
        self.setFunc(func, args)

    def setFunc(self, func, args):
        '''
        setFunc() is the method that sets the function
        to be called as well as its arguments
        '''
        self.func = func
        self.args = args

    def getResult(self):
        '''
        getResult() provides the return value
        from the function call
        '''
        return self.res

    def run(self):
        '''
        save the return value from the function call
        into an instance var rather than returning it
        '''
        print 'starting', self.name, 'at:', ctime(time())
        # * 1.6 * self.res = self.func(*self.args)
        self.res = apply(self.func, self.args)
        print self.name, 'finished at:', ctime(time())

