发布时间:2019-09-03 09:01:34编辑:auto阅读(2656)
import threading
import time
"""重新定义带返回值的线程类"""
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
"""测试函数,计算两个数之和"""
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() # 一定要join,不然主线程比子线程跑的快,会拿不到结果
print (t.get_result())
上一篇: python清空屏幕
下一篇: 【Python】保留有效位
47864
46428
37315
34763
29333
25992
24946
19968
19566
18052
5807°
6434°
5950°
5976°
7081°
5927°
5965°
6457°
6421°
7801°