继承threading.Thread类,添加get_result方法
def strTest(strName):
strText = strName + " test"
print("strTest func....")
return strText, strName
class MyThread(threading.Thread):
def __init__(self, func, args):
super().__init__()
self.func = func
self.args = args
def run(self):
self.result1, self.result2 = self.func(*self.args)
def get_result(self):
try:
return self.result1, self.result2
except Exception:
return None
if __name__ == '__main__':
hThread = MyThread(strTest, args=('123',))
hThread.start()
hThread.join()
res1, res2 = hThread.get_result()
print(res1)
print(res2)
输出结果: