多线程系列:线程中包含子线程
这里展示的多线程基本包含了常见的多线程操作:
- 实现线程中包含子线程;
- 计算子线程耗时
- 展示一个函数如何通过参数传递进行多线程
- 一个线程调用一个函数的情况
代码示例
import threading
import time
from pprint import pprint
import queue
# 假设以下是二个分类模型函数
def classify_model_1(image_path, queue):
# 模拟分类操作
time.sleep(1)
print("classify_model_1")
# return f"模型1分类结果: {image_path}"
queue.put(f"模型1分类结果: {image_path}")
def classify_model_2(image_path, queue):
# 模拟分类操作
time.sleep(1)
print("classify_model_2")
# return f"模型2分类结果: {image_path}"
queue.put(f"模型2分类结果: {image_path}")
# 假设以下是二个分割模型函数
def segment_model_1(image_path, queue):
# 模拟分割操作
time.sleep(1)
print("segment_model_1")
# return f"模型1分割结果: {image_path}"
queue.put(f"模型1分割结果: {image_path}")
def segment_model_2(image_path, queue):
# 模拟分割操作
time.sleep(1)
print("segment_model_2")
# return f"模型2分割结果: {image_path}"
queue.put(f"模型2分割结果: {image_path}")
# 线程执行函数
def process_image(image_path, result_dict, result_key):
# 创建用于线程间通信的队列
classify_queue = queue.Queue()
segment_queue = queue.Queue()
# 创建分类模型和分割模型的线程
classify_threads = [
threading.Thread(target=classify_model_1, args=(image_path, classify_queue)),
threading.Thread(target=classify_model_2, args=(image_path, classify_queue))
]
segment_threads = [
threading.Thread(target=segment_model_1, args=(image_path, segment_queue)),
threading.Thread(target=segment_model_2, args=(image_path, segment_queue))
]
start_time_dict = {}
run_time_dict = {}
# 启动所有线程
for t in classify_threads + segment_threads:
t.start()
start_time_dict[t.ident] = time.time()
# 等待所有分类模型线程完成并记录结束时间
for t in classify_threads:
t.join()
run_time_dict[t.ident] = time.time() - start_time_dict[t.ident]
# 等待所有分割模型线程完成并记录结束时间
for t in segment_threads:
t.join()
run_time_dict[t.ident] = time.time() - start_time_dict[t.ident]
# 收集所有分类和分割模型的结果
classify_results = []
while not classify_queue.empty():
classify_results.append(classify_queue.get())
segment_results = []
while not segment_queue.empty():
segment_results.append(segment_queue.get())
# 将结果存储在共享字典中
result_dict[result_key] = (classify_results, segment_results, run_time_dict)
# 主函数
if __name__ == "__main__":
tic = time.time()
# 假设我们有一个图像路径列表
image_paths = [
"image1.jpg",
"image2.jpg",
]
result_dict = {}
threads = []
for i, image_path in enumerate(image_paths):
t = threading.Thread(target=process_image, args=(image_path, result_dict, i))
threads.append(t)
t.start()
# 等待所有线程完成
for t in threads:
t.join()
# 输出结果
for key, value in result_dict.items():
pprint(value)
toc = time.time()
run_time = toc - tic
print("程序运行时间:", run_time)
结果示例
# classify_model_1
# classify_model_2
# classify_model_1
# classify_model_2
# segment_model_1
# segment_model_2
# segment_model_1
# segment_model_2
# (['模型1分类结果: image2.jpg', '模型2分类结果: image2.jpg'],
# ['模型2分割结果: image2.jpg', '模型1分割结果: image2.jpg'],
# {201612: 1.0070557594299316,
# 204744: 1.0125854015350342,
# 205424: 1.0160560607910156,
# 207048: 1.0160560607910156})
# (['模型1分类结果: image1.jpg', '模型2分类结果: image1.jpg'],
# ['模型1分割结果: image1.jpg', '模型2分割结果: image1.jpg'],
# {204448: 0.9758126735687256,
# 206648: 0.9813151359558105,
# 207700: 1.0226755142211914,
# 207812: 1.0472991466522217})
# 程序运行时间: 1.266035556793213
标签:系列,image,queue,线程,time,model,多线程,segment,classify
From: https://blog.csdn.net/familytaijun/article/details/141036431