python 多线程汇总处理结果
本文关键字: python, Thread, 多线程1,安装对应库
2,创建自定义线程类
3,创建线程,启动并返回结果汇总处理。
import threading
import time
from time import ctime, sleep
from threading import Thread
import platformprint(platform.architecture())
def buy(t,s):
print(“buy start”)
time.sleep(t)
print(“buy done”)
return sdef sell(t,s):
print(“sell start”)
time.sleep(t)
print(“sell done”)
return sclass MyThread(Thread):
def __init__(self,func,args=()):
super(MyThread,self).__init__()
self.func = func
self.args = argsdef run(self):
self.result = self.func(*self.args)def get_result(self):
return self.resultthd1 = 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)