from threading import Thread class MyThread(Thread): def __init__(self, func, args=()): Thread.__init__(self) self.func = func self.args = args self.result = self.func(self.args) # 获取子线程结果 def result(self): # 必须阻塞子线程才能获取到结果 Thread.join(self) try: return self.result except Exception as e: print('获取子线程结果失败: %s' % e) return None def test(arg): return arg if __name__ == '__main__': # 开启子线程 thread = MyThread(test, ('param',)) thread.setDaemon(True) thread.start() # 获取返回结果 result = MyThread.result(thread) print(result) input(111)
标签:__,Thread,python,self,args,获取,线程,result From: https://www.cnblogs.com/wuyuchuan/p/17132724.html