- Mulitprocessing. Pool initialization parameter to specify the number of threads in the process pool
- Add Task Obj.apply_async (func, (ARG)) to the process pool
- You need to call the Close method of the process pool after you finish adding tasks
- The pool process is implemented in such a way that the main process does not wait, shuts down after execution, and causes the child process to not execute, so the join method needs to be called
"""python 进程池"""from multiprocessing import Process, Poolimport os# create a new process pool with arg 2 means the process pool contain 2 processpool = Pool(2)# 使用多进程执行的任务def test(arg): print("%d---work,work...this is %s process"%(arg, os.getpid()))for i in range(20): print("putting job %d"%i) pool.apply_async(test, (i,)) # async异步执行pool.close() # 关闭进程池,相当于 不能够再次添加新任务了pool.join()# 当主进程的任务做完之后 立马结束,,,如果这个地方没join,会导致# 进程池中的任务不会执行print("main process runing.....")
Python's multi-process pool