Pythonマルチスレッドの戻り値の問題を解決します



Solve Problem Return Value Python Multithreading



数日前にPythonのマルチスレッド化を見ましたが、作成されたスレッドが関数の戻り値を取得できないことがわかりました。情報を見ると、Threadクラスを書き直して、コードを直接追加する必要があることがわかりました。 import threading import time '' 'Redefine thread class with return value' '' class MyThread(threading.Thread): def __init__(self,func,args=()): super(MyThread,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 except Exception: return None '' 'Test function, calculate the sum of two numbers' '' def fun(a,b): time.sleep(1) return a+b li = [] for i in range(4): t = MyThread(fun,args=(i,i+1)) li.append(t) t.start() for t in li: t.join () # Be sure to join, otherwise the main thread will run faster than the child thread and you will not get results print (t.get_result())