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
34
35
import
threading
class
MyThread(object):
def
__init__(self, func_list=None):
#
所有线程函数的返回值汇总,如果最后为
0
,说明全部成功
self.ret_flag =
0
self.func_list =
func_list
self.threads =
[]
def
set_thread_func_list(self, func_list):
"""
@note: func_list
是一个
list
,每个元素是一个
dict
,有
func
和
args
两个参数
"""
self.func_list =
func_list
def
start(self):
"""
@note:
启动多线程执行,并阻塞到结束
"""
self.threads =
[]
self.ret_flag =
0
for
func_dict in
self.func_list:
if
func_dict["args"]:
t =
threading.Thread(target=func_dict["func"],
args=func_dict["args"])
else:
t =
threading.Thread(target=func_dict["func"])
self.threads.append(t)
for
thread_obj in
self.threads:
thread_obj.start()
for
thread_obj in
self.threads:
thread_obj.join()