多线程系列:自定义多线程类获取函数返回值
自定义多线程类参考博客: https://blog.csdn.net/xpt211314/article/details/109543014.
这里展示的多线程示例包括:
- 子线程包括多线程的情况
- 计算子线程耗时情况
- 多个线程对应一一对应不同的函数情况
- 多个线程对应同一个函数,只是输入参数不一样情况。
代码示例
python
import threading
import time
from pprint import pprint
class MyThread(threading.Thread):
def __init__(self, target, args=()):
super(MyThread, self).__init__()
self.func = target
self.args = args
def run(self):
self.result = self.func(*self.args)
def get_result(self):
threading.Thread.join(self) # 等待线程执行完毕
try:
return self.result
except Exception as e:
return e
# 定义分类模型函数
def classify_model_1(image_path):
# 模拟分类操作
time.sleep(1)
print("classify_model_1")
return {f"模型1_{image_path}分类结果": image_path}
def classify_model_2(image_path):
# 模拟分类操作
time.sleep(1)
print("classify_model_2")
return {f"模型2_{image_path}分类结果": image_path}
def classify_model_3(image_path):
# 模拟分类操作
time.sleep(1)
print("classify_model_3")
return {f"模型3_{image_path}分类结果": image_path}
def classify_model_4(image_path):
# 模拟分类操作
time.sleep(1)
print("classify_model_4")
return {f"模型4_{image_path}分类结果": image_path}
def classify_model_5(image_path):
# 模拟分类操作
time.sleep(1)
print("classify_model_5")
return {f"模型5_{image_path}分类结果": image_path}
# 定义分割模型函数
def segment_model_1(image_path):
# 模拟分割操作
time.sleep(1)
print("segment_model_1")
return {f"模型1_{image_path}分割结果": image_path}
def segment_model_2(image_path):
# 模拟分割操作
time.sleep(1)
print("segment_model_2")
return {f"模型2_{image_path}分割结果": image_path}
def segment_model_3(image_path):
# 模拟分割操作
time.sleep(1)
print("segment_model_3")
return {f"模型3_{image_path}分割结果": image_path}
def segment_model_4(image_path):
# 模拟分割操作
time.sleep(1)
print("segment_model_4")
return {f"模型4_{image_path}分割结果": image_path}
def segment_model_5(image_path):
# 模拟分割操作
time.sleep(1)
print("segment_model_5")
return {f"模型5_{image_path}分割结果": image_path}
def process_image(image_path):
# 创建线程列表
model_thread_list = [
MyThread(classify_model, args=(image_path,))
for classify_model in [
classify_model_1, classify_model_2, classify_model_3,
classify_model_4, classify_model_5
]
]
model_thread_list.extend([
MyThread(segment_model, args=(image_path,))
for segment_model in [
segment_model_1, segment_model_2, segment_model_3,
segment_model_4, segment_model_5
]
])
start_time_dict = {}
run_time_dict = {}
# 启动线程
for t in model_thread_list:
t.start()
start_time_dict[t.ident] = time.time()
# 等待所有线程完成
for t in model_thread_list:
t.join()
run_time_dict[t.ident] = time.time() - start_time_dict[t.ident]
# 获取结果
model_result_dict = {}
for t in model_thread_list:
model_result_dict.update(t.get_result())
# print(results)
model_result_dict.update(run_time_dict)
return model_result_dict
if __name__ == '__main__':
tic = time.time()
image_path_list = [
"image1.jpg",
"image2.jpg",
]
image_thread_list = [
MyThread(process_image, args=(image_path,))
for image_path in image_path_list
]
for t in image_thread_list:
t.start()
result_dict = {}
for t in image_thread_list:
t.join()
result_dict.update(t.get_result())
pprint(result_dict)
toc = time.time()
print("程序运行时间:", toc - tic)
结果示例
python
# segment_model_2
# segment_model_5
# segment_model_2
# segment_model_1
# segment_model_1
# segment_model_3
# segment_model_4
# classify_model_5
# segment_model_4
# segment_model_5
# segment_model_3
# classify_model_3
# classify_model_2
# classify_model_3
# classify_model_4
# classify_model_1
# classify_model_1
# classify_model_2
# classify_model_5
# classify_model_4
# {88328: 1.0205564498901367,
# 110924: 1.0205564498901367,
# 132612: 1.0205564498901367,
# 139284: 1.0205564498901367,
# 140332: 1.0205564498901367,
# 140392: 1.0205564498901367,
# 140404: 1.0205564498901367,
# 140756: 1.0205564498901367,
# 140920: 1.0205564498901367,
# 141028: 1.0205564498901367,
# 141076: 1.0205564498901367,
# 141252: 1.0205564498901367,
# 141416: 1.0205564498901367,
# 141628: 1.0205564498901367,
# 141964: 1.0205564498901367,
# 142716: 1.0205564498901367,
# 143008: 1.0205564498901367,
# 143148: 1.0205564498901367,
# 143312: 1.0205564498901367,
# 144176: 1.0205564498901367,
# '模型1_image1.jpg分割结果': 'image1.jpg',
# '模型1_image1.jpg分类结果': 'image1.jpg',
# '模型1_image2.jpg分割结果': 'image2.jpg',
# '模型1_image2.jpg分类结果': 'image2.jpg',
# '模型2_image1.jpg分割结果': 'image1.jpg',
# '模型2_image1.jpg分类结果': 'image1.jpg',
# '模型2_image2.jpg分割结果': 'image2.jpg',
# '模型2_image2.jpg分类结果': 'image2.jpg',
# '模型3_image1.jpg分割结果': 'image1.jpg',
# '模型3_image1.jpg分类结果': 'image1.jpg',
# '模型3_image2.jpg分割结果': 'image2.jpg',
# '模型3_image2.jpg分类结果': 'image2.jpg',
# '模型4_image1.jpg分割结果': 'image1.jpg',
# '模型4_image1.jpg分类结果': 'image1.jpg',
# '模型4_image2.jpg分割结果': 'image2.jpg',
# '模型4_image2.jpg分类结果': 'image2.jpg',
# '模型5_image1.jpg分割结果': 'image1.jpg',
# '模型5_image1.jpg分类结果': 'image1.jpg',
# '模型5_image2.jpg分割结果': 'image2.jpg',
# '模型5_image2.jpg分类结果': 'image2.jpg'}
# 程序运行时间: 1.0205564498901367