#!/usr/bin/env python

from __future__ import print_function
from myThread import MyThread
from time import ctime, sleep

def fib(x):
    sleep(0.005)
    if (x < 2):
        return 1
    return (fib(x - 2) + fib(x - 1))

def fac(x):
    sleep(0.1)
    if (x < 2):
        return 1
    return (x * fac(x - 1))

def mysum(x):
    sleep(0.1)
    if (x < 2):
        return 1
    return (x + mysum(x - 1))

funcs = [fib, fac, mysum]
n = 12

def main():
    nfuncs = range(len(funcs))

    print("***SINGLE THREAD***")
    for i in nfuncs:
        print("starting %s at: %s" % (funcs[i].__name__, ctime()))
        print(funcs[i](n))
        print("%s finished at: %s" % (funcs[i].__name__, ctime()))

    print("***MULTIPLE THREADS***")
    threads = []
    for i in nfuncs:
        t = MyThread(funcs[i], (n,), funcs[i].__name__)
        threads.append(t)

    for i in nfuncs:
        threads[i].start()

    for i in nfuncs:
        threads[i].join()
        print(threads[i].get_result())

    print("all done")

if __name__ == "__main__":
    main()


