from concurrent import futures import time import os
print("cpu 数量:", os.cpu_count()) # 测试笔记本是4核
deftest(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)