python 多线程汇总处理结果



1,安装对应库
2,创建自定义线程类
3,创建线程,启动并返回结果汇总处理。

import threading
import time
from time import ctime, sleep
from threading import Thread
import platform

print(platform.architecture())

def buy(t,s):
print(“buy start”)
time.sleep(t)
print(“buy done”)
return s

def sell(t,s):
print(“sell start”)
time.sleep(t)
print(“sell done”)
return s

class MyThread(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):
return self.result

thd1 = MyThread(buy,(3,1,))
thd2 = MyThread(sell,(5,1,))
thd1.start()
thd2.start()
thd1.join()
thd2.join()
print(“all done”)
res=thd1.get_result()+thd2.get_result()

print(res)