from multiprocessing import Process,Pool
import time,os
def Foo(i):
time.sleep(1)
print("在子进程中",os.getpid())
return i+100
def Bar(arg):
print("执行id",os.getpid())
if __name__=="__main__":
print(os.getpid())
pool=Pool(5)#允许进程池同时放入5个进程
for i in range(50):
pool.apply_async(func=Foo,args=(i,),callback=Bar)#注意apply_async是并行执行的意思,callback代表回调,也就是执行完Foo后再执行Bar,特别注意,调用callback里的函数是父进程,而不是子进程
#pool.apply(func=Foo,args=(i,))#apply是串行执行,不支持callback
print("end")
pool.close()
pool.join()#这里需要等待进程池的所有进程执行完毕,如果没有这个,关闭进程后,程序就停掉了,进程还没执行完
python进程池
猜你喜欢
转载自blog.csdn.net/qq_37181884/article/details/82291349
今日推荐
周排行