进程池VS线程池
# CPU 密集:大量纯计算(加解密、图像处理、数值分析)。需要真·多核并行来堆算力。 【进程】
# I/O 密集:大量等待(网络、磁盘、数据库)。CPU 大部分时间都在闲着,关键是别浪费“等”的时间。 【线程】
结果分析
#### 结果
I/O密集型:线程池进度: 100%|██████████| 3/3 [00:02<00:00, 1.00it/s]
I/O密集型:进程池进度: 100%|██████████| 3/3 [00:03<00:00, 1.02s/it]
I/O密集型任务: Thread线程池消耗时间: 3.01 结果: ['task0', 'task1', 'task2']
I/O密集型任务: Process进程池消耗时间: 3.1 结果: ['task0', 'task1', 'task2']
CPU计算型:线程池进度: 100%|██████████| 3/3 [00:00<00:00, 4.26it/s]
CPU计算型:进程池进度: 100%|██████████| 3/3 [00:00<00:00, 9.81it/s]
CPU计算型任务: Thread线程池消耗时间: 0.77 结果: [50000005000000, 50000005000000, 50000005000000]
CPU计算型任务 Process进程池消耗时间: 0.32 结果: [50000005000000, 50000005000000, 50000005000000]
示例代码
# -*- coding: utf-8 -*-
import time
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ProcessPoolExecutor
from tqdm import tqdm
# --- 模拟IO操作 ---
def fake_io(idx):
time.sleep(3)
return f"task{idx}"
# --- 模拟CPU计算 ---
# 或者如果需要保持循环结构但优化性能:
def fake_cpu_optimized(n):
result = 0
# 使用局部变量加速循环
# 使用步长优化或向量化思想
for i in range(1, n + 1):
result += i
return result
def timmer(func):
start_time = time.time()
result = func()
end_time = time.time()
return round(end_time - start_time, 2), result
if __name__ == '__main__':
# I/O : 线程池VS进程池子
def io_with_threads():
with ThreadPoolExecutor(max_workers=3) as executor:
result = list(tqdm(executor.map(fake_io, range(3)), total=3, desc="I/O密集型:线程池进度"))
return result
def io_with_processes():
with ProcessPoolExecutor(max_workers=3) as executor:
result = list(tqdm(executor.map(fake_io, range(3)), total=3, desc="I/O密集型:进程池进度"))
return result
t_thread_io, result_thread_io = timmer(io_with_threads)
t_process_io, result_process_io = timmer(io_with_processes)
print(f"I/O密集型任务: Thread线程池消耗时间: {t_thread_io} 结果: {result_thread_io}")
print(f"I/O密集型任务: Process进程池消耗时间: {t_process_io} 结果: {result_process_io}")
# CPU : 线程池VS进程池
def cpu_with_threads():
# 创建3个相同的计算任务
tasks = [10000000] * 3 # 每个任务计算1千万
with ThreadPoolExecutor(max_workers=3) as executor:
# 使用tqdm显示进度
results = list(tqdm(executor.map(fake_cpu_optimized, tasks),
total=len(tasks),
desc="CPU计算型:线程池进度"))
return results
def cpu_with_processes():
tasks = [10000000] * 3 # 每个任务计算1千万
with ProcessPoolExecutor(max_workers=3) as executor:
results = list(tqdm(executor.map(fake_cpu_optimized, tasks),
total=len(tasks),
desc="CPU计算型:进程池进度"))
return results
# 修改计时器函数以返回结果
t_thread_cpu, thread_results = timmer(cpu_with_threads)
t_process_cpu, process_results = timmer(cpu_with_processes)
print(f"CPU计算型任务: Thread线程池消耗时间: {t_thread_cpu} 结果: {thread_results}")
print(f"CPU计算型任务 Process进程池消耗时间: {t_process_cpu} 结果: {process_results}")
[学习指南]
https://zhuanlan.zhihu.com/p/1922709704695590980