赞
踩
主要练习使用多进程ProcessPoolExecutor对CPU密集型任务进行优化
import time from multiprocessing import Process, Queue, Lock from concurrent.futures import ProcessPoolExecutor def func(x): ''' CPU密集型任务 单个耗时2.94 seconds ''' for i in range(50000000): x += i return x if __name__ == '__main__': t0 = time.time() with ProcessPoolExecutor() as executor: results = executor.map(func, [1, 2, 3]) for result in results: print(result) t1 = time.time() print("ProcessPoolExecutor多进程运行cost:", t1 - t0, "seconds")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。