进程池 Pool

from multiprocessing import Process,Pool

import time


#最后一定要 close 和 join

def fun(a):
    time.sleep(1)
    print(a)
    return a+100


def bar(arg):
    print('----',arg)

if __name__ == '__main__':

    pool = Pool(5)

    for i in range(100):
        pool.apply_async(func = fun,args=(i,),callback = bar)

    pool.close()
    pool.join()

  

apply 每一个任务是排队进行
apply_async 每一个任务是并发进行,可设置回调函数,最后必须close join 

  

原文地址:https://www.cnblogs.com/cloniu/p/6283806.html