python获取threading多线程的返回值
import threading import os import sys class TestThread(threading.Thread): def __init__(self, func, args=()): super(TestThread, self).__init__() self.func = func self.args = args def run(self): self.result = self.func(*self.args) def get_result(self): try: return self.result # 如果子线程不使用join方法,此处可能会报没有self.result的错误 except Exception: return None def add(num): print(num) return num+1000 def task_run(): try: p = [] for i in range(5): p.append(TestThread(func=add, args=(i,))) for j in p: j.start() for j in p: j.join() res = j.get_result() print(res) except Exception as err: print(str(err))