python简单多线程:返回执行结果
简单多线程返回结果:
import threading import time def add(x,y): print("add") return x+y def multi(x,y): print("multi") return x*y class WorkThread(threading.Thread): """ 继承线程类,重写run方法 """ def __init__(self,func,x,y): super(WorkThread,self).__init__() self.func = func self.x = x self.y = y def run(self): # time.sleep(3) ret = self.func(self.x,self.y) # 打印结果 print(ret) if __name__ == "__main__": print("start") wt_obj = WorkThread(add,1,2) wt_obj1 = WorkThread(multi,3,4) wt_obj.start() # 执行完子线程再结束主线程 wt_obj.join() # 不等子线程是否结束,主线程结束就结束 # wt_obj.setDaemon(True) wt_obj1.start() wt_obj1.join() # wt_obj1.setDaemon(True) print("end") # 结果: # start # add # 3 # multi # 12 # end