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+100 11 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()