1 #!/usr/bin/env python 2 3 from multiprocessing import Process,Pool,freeze_support 4 5 import time,os 6 7 def Foo(i): 8 time.sleep(1) 9 print("in process",os.getpid())10 return i+10011 12 def Bar(arg):13 print("--exec done--",arg)14 15 if __name__ =="__main__":16 pool = Pool(5) # 允许进程池中同时放入5个进程17 for i in range(10):18 # pool.apply(func=Foo,args=(i,)) # 同步执行19 pool.apply_async(func=Foo,args=(i,),callback=Bar) # 异步执行 callback 回调函数20 # 先关闭21 pool.close()22 # 等待进程执行完毕,在关闭23 pool.join()