进程池 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 

  

posted @ 2017-01-13 18:51  200ML  阅读(180)  评论(0编辑  收藏  举报