进程池有效的利用了进程
举例:
from multiprocessing import Pool,current_process
import time,random
def func(num):
t1 = time.time()
print('%s,,,begin...%s'%(current_process().name,num))
time.sleep(random.random())
t2 = time.time()
print('%s,,,end...time=%0.1f' % (current_process().name,t2-t1))
if name == 'main':
print('主进程开始....')
#创建进程池
my_pool = Pool(4)
for i in range(100):
#添加任务
my_pool.apply_async(func,(i,))
# 等一个子程序运行完成之后,在开始下一个
# my_pool.apply(func,(i,))
#关闭
my_pool.close()
#开始
my_pool.join()
print('主进程结束....')