python线程

获取线程执行结果

通过继承自threading.Thread,重写其run方法来实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import threading


class MyThread(threading.Thread):
def __init__(self, *args, **kwargs):
"""
使用父类构造器,尽量保持语法一致,仅将线程执行结果缓存
"""

#设定回调
if 'callable' in kwargs:
self._callable = kwargs['callable']
del kwargs['callable']
super(MyThread, self).__init__(*args, **kwargs)
self._result = None
pass

def run(self):
if self._target:
self._result = self._target(*self._args, **self._kwargs)

def result(self, _callable=None):
"""
join()确保任务已经执行完成
:return:
"""
self.join()
# 回调
if _callable is None:
_callable = self._callable
if _callable:
_callable(self._result)
return self._result

测试程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import threading
import time

def foo():
time.sleep(1)
return time.time()


tasks = []
for i in range(10):
tasks.append(MyThread(target=foo))
for t in tasks:
t.start()

for t in tasks:
t.result()