Python 线程池 和 进程池 使用

[TOC]

概念

  • 并发:串行
  • 并行:并排运行

并行编程测试

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
36
37
38
39
40
41
42
43
44
from concurrent import futures
import time
import os


print("cpu 数量:", os.cpu_count()) # 测试笔记本是4核


def test(id, num): # 涉及到多参数问题
time.sleep(num)
print(f"task {id} finished")
return id



# 线程池 默认的是cpu的数目*5
start = time.time()
with futures.ThreadPoolExecutor() as tp:
task_list = [tp.submit(test, i, i) for i in range(8, 0, -1)]
# task_result = [t.result() for t in task_list] # 有任务结束,就返回,有序结果
task_result = [t.result() for t in futures.as_completed(task_list)] # 有任务结束,就返回结果,无序
print(task_result)
print(time.time() - start)


start = time.time()
with futures.ThreadPoolExecutor() as tp:
task_result = tp.map(lambda args: test(*args), [(i, i) for i in range(8, 0, -1)])
print(list(task_result)) # 原始顺序
print(time.time() - start)


# 进程池 默认的是cpu的数目
start = time.time()
with futures.ProcessPoolExecutor() as pp:
task_list = [pp.submit(test, i, i) for i in range(8, 0, -1)]
# task_result = [t.result() for t in task_list] # 有任务结束,就返回,有序结果
task_result = [t.result() for t in futures.as_completed(task_list)] # 有任务结束,就返回,无序结果
print(task_result)
print(time.time() - start)

# 进程池的map测试,需要修改测试函数

# 测试结果:线程池速度优于进程池

参考

坚持原创技术分享,您的支持将鼓励我继续创作!
0%