import math
import threading,time
from tools.listOperation import *


class MyThread(threading.Thread):
    def __init__(self,func,divlist,**kwargs):
        super(MyThread,self).__init__()
        self.func=func
        self.divlist=divlist
        self.result=None
        self.kv=kwargs

    def run(self):
        #time.sleep(1)
        #print(threading.currentThread().name)
        #print('请求参数：',self.kv,self.divlist,self.func)
        if self.kv:
            t=self.func(self.divlist,self.kv)
        else:
            t=self.func(self.divlist)
        #print('结果:',t)
        self.result=t
        #print('over')

    def get_result(self):
        #print(self.name,'-->multhread:',self.result)
        return self.result

def tttt(ldata,kwargs={}):
    print('1==',ldata)
    print('2==',kwargs)
    flag1=kwargs.get('init_data') or 0
    for i in ldata:
        return i+flag1

def main(thnum,list_data,func,**kwargs):
    divlists=split_by_num(list_data,thnum)
    temp_threads=[]
    mult_thread_result=[]
    for i in range(thnum):
        t=MyThread(func,divlists[i],**kwargs)
        temp_threads.append(t)
        t.start()
    for i in temp_threads:
        i.join()
    for i in temp_threads:
        mult_thread_result.append(i.get_result())
    return mult_thread_result




if __name__ == '__main__':
     flag={"init_data":10}
     list_data=[i for i in range(100)]
     t=time.time()
     result=main(5,list_data,tttt,**flag)
     tt=time.time()
     print(tt-t)
     print('final',result)
     # print("=="*50)
     # t=time.time()
     # for i in list(range(100)):
     #     print(i)
     # tt=time.time()
     # print(tt-t)







