3.3.2 进程池
进程池
进程池内部维护一个进程序列,当使用时,则去进程池中获取一个进程,如果进程池序列中没有可供使用的进进程,那么程序就会等待,直到进程池中有可用进程为止。
进程池中有两个方法:
- apply
- apply_async
串行示例
from multiprocessing import Process, Pool import time, os def f(i): time.sleep(1) print('Process: ', os.getpid()) #2每个进程输出当前进程ID return i + 100 def bar(arg): print('Process %s execution is done!', arg % os.getpid()) if __name__ == '__main__': p = Pool(5) #1启动一个池子,池子最多同时允许5个进程工作 for i in range(10): p.apply(func=f, args=(i, )) #使用apply()方法,池子中的进程按串行执行 print('End') #3 p.close()
结果
Process: 3804 Process: 11880 Process: 3740 Process: 128 Process: 644 Process: 3804 Process: 11880 Process: 3740 Process: 128 Process: 644 End
并行示例
from multiprocessing import Process, Pool import time, os def f(i): time.sleep(1) print('Process: ', os.getpid()) #2.1每个进程输出当前进程ID return i + 100 def bar(arg): print('Process %s execution is done!', arg % os.getpid()) #2.2执行callback if __name__ == '__main__': p = Pool(5) #1启动一个池子,池子最多同时允许5个进程工作 for i in range(10): p.apply_async(func=f, args=(i, ), callback=bar) #使用apply_async()方法,池子中的进程按并行执行 print('End') #3 p.close() p.join()
结果
End Process: Process: 14420 7708 Process %s execution is done! 101 Process %s execution is done! 100 Process: 14936 Process %s execution is done! 102 Process: 9632Process: 14096 Process %s execution is done! 103 Process %s execution is done! 104 Process: Process: 14420 7708 Process %s execution is done! 105 Process %s execution is done! 106 Process: 14936 Process %s execution is done! 107 Process: Process: 9632 14096 Process %s execution is done! 108 Process %s execution is done! 109
每次蹦出5条结果