进程池无IO堵塞的情况
# coding:utf-8 import time from multiprocessing import Process, Pool def func(n): pass if __name__ == ‘__main__‘: num = 10 start_pool_time = time.time() pool = Pool(5) pool.map(func, range(num)) # map是异步执行的,并且自带close和join print("通过进程池执行的时间:", time.time() - start_pool_time) std_start_time = time.time() for i in range(num): pass print("正常执行的执行时间:", time.time() - std_start_time) pro_start_time = time.time() p_lst = [] for i in range(num): p = Process(target=func, args=(i,)) p.start() p_lst.append(p) [pp.join() for pp in p_lst] print("多进程的执行时间:", time.time() - pro_start_time) # 通过进程池执行的时间: 0.46875 # 正常执行的执行时间: 0.0 # 多进程的执行时间: 0.828125 #一般约定俗成的是进程池中的进程数量为CPU的数量,工作中要看具体情况来考量。
有IO阻塞的情况
# coding:utf-8 import time from multiprocessing import Process, Pool def func(n): time.sleep(1) if __name__ == ‘__main__‘: num = 10 start_pool_time = time.time() pool = Pool(5) pool.map(func, range(num)) print("通过进程池执行的时间:", time.time() - start_pool_time) std_start_time = time.time() for i in range(num): time.sleep(1) print("正常执行的执行时间:", time.time() - std_start_time) pro_start_time = time.time() p_lst = [] for i in range(num): p = Process(target=func, args=(i,)) p.start() p_lst.append(p) [pp.join() for pp in p_lst] print("多进程的执行时间:", time.time() - pro_start_time) # 通过进程池执行的时间: 2.578125 # 正常执行的执行时间: 10.0 # 多进程的执行时间: 1.75
原文地址:https://www.cnblogs.com/lilyxiaoyy/p/10986358.html
时间: 2024-11-10 00:39:20