进程池: python
在利用Python进行系统管理的时候,特别是同时操做多个文件目录,或者远程控制多台主机,并行操做能够节约大量的时间。当被操做对象数目不大时,能够直接利用multiprocessing中的Process动态成生多个进程,十几个还好,但若是是上百个,上千个目标,手动的去限制进程数量却又太过繁琐,此时能够发挥进程池的功效。 并发
Pool能够提供指定数量的进程供用户调用,当有新的请求提交到pool中时,若是池尚未满,那么就会建立一个新的进程用来执行该请求;但若是池中的进程数已经达到规定最大值,那么该请求就会等待,直到池中有进程结束,才会建立新的进程来它。app
如何使用进程池?async
1 如何使用进程池执行函数?ide
a 不返回参数函数
# -*- coding: UTF-8 -*- from multiprocessing import Process,Manager,Lock,Pool #要在调用进程池执行的函数 def sayHi(num): print "def print result:",num #进程池最大运行数 p = Pool(processes=4) #模拟并发调用线程池 for i in range(10): p.apply_async(sayHi,[i]) 执行结果: # python demo.py def print result: 0 def print result: 1 def print result: 2 def print result: 3 def print result: 4 def print result: 5
apply_async(func[, args[, kwds[, callback]]]) 它是非阻塞,apply(func[, args[, kwds]])是阻塞的(理解区别,看例1例2结果区别)spa
2 进程池使用之坑~~线程
# -*- coding: UTF-8 -*- from multiprocessing import Process,Manager,Lock,Pool #要在调用进程池执行的函数 def sayHi(num): print "def print result:",num #进程池最大运行数 p = Pool(processes=4) #模拟并发调用线程池 for i in range(10): p.apply_async(sayHi,[i])
执行结果:orm
[root@python thread]# python pool.py def print result: 0 def print result: 1 def print result: 2 def print result: 3 def print result: 4 def print result: 5 [root@python thread]# python pool.py def print result: 0 def print result: 1 def print result: 2 def print result: 3 def print result: 4 def print result: 5 def print result: 6 [root@python thread]# python pool.py [root@python thread]# python pool.py [root@python thread]# python pool.py
从上面的例子能够看出,咱们连续执行pool.py脚本,后面的脚本却没有输出应有的结果,为何?对象
首先对上列程序进行细微调整:
# -*- coding: UTF-8 -*- from multiprocessing import Process,Manager,Lock,Pool def sayHi(num): print "def print result:",num p = Pool(processes=4) for i in range(10): p.apply_async(sayHi,[i]) p.close() p.join() #调用join以前,先调用close函数,不然会出错。执行完close后不会有新的进程加入到pool,join函数等待全部子进程结束
返回结果:
[root@python thread]# python pool.py def print result: 0 def print result: 1 def print result: 2 def print result: 3 def print result: 4 def print result: 5 def print result: 6 def print result: 9 def print result: 8 def print result: 7 [root@python thread]# python pool.py def print result: 0 def print result: 1 def print result: 2 def print result: 4 def print result: 3 def print result: 5 def print result: 6 def print result: 7 def print result: 8 def print result: 9 [root@python thread]# python pool.py def print result: 0 def print result: 1 def print result: 2 def print result: 3 def print result: 4 def print result: 5 def print result: 7 def print result: 8 def print result: 9
此次执行彻底没有问题,那么为什么加入close()和join()方法后就会执行正确呢?
close() 关闭pool,使其不在接受新的任务。
terminate() 结束工做进程,不在处理未完成的任务。
join() 主进程阻塞,等待子进程的退出, join方法要在close或terminate以后使用。
原来重点是join方法,若是不阻塞主进程,会致使主进程往下运行到结束,子进程都尚未返回结果
3 进程池调用后返回参数
# -*- coding: UTF-8 -*- from multiprocessing import Process,Manager,Lock,Pool def sayHi(num): return num*num p = Pool(processes=4) #申明一个列表,用来存放各进程返回的结果 result_list =[] for i in range(10): result_list.append(p.apply_async(sayHi,[i])) #将返回结果append到列表中 #循环读出列表返回的结果 for res in result_list: print "the result:",res.get()
注:get()函数得出每一个返回结果的值
执行结果:
[root@python thread]# python pool.py the result: 0 the result: 1 the result: 4 the result: 9 the result: 16 the result: 25 the result: 36 the result: 49 the result: 64 the result: 81 [root@python thread]# python pool.py the result: 0 the result: 1 the result: 4 the result: 9 the result: 16 the result: 25 the result: 36 the result: 49 the result: 64 the result: 81 [root@python thread]# python pool.py the result: 0 the result: 1 the result: 4 the result: 9 the result: 16 the result: 25 the result: 36 the result: 49 the result: 64
将结果经过return返回后,写入列表后,而后再循环读出,你会发现及时不须要join方法,脚本仍然能正常显示。
可是为了代码更加稳定,仍是建议增长主进程阻塞(除非主进程须要等待子进程返回结果):
# -*- coding: UTF-8 -*- from multiprocessing import Process,Manager,Lock,Pool def sayHi(num): return num*num p = Pool(processes=4) #申明一个列表,用来存放各进程返回的结果 result_list =[] for i in range(10): result_list.append(p.apply_async(sayHi,[i])) #将返回结果append到列表中 p.close() p.join() #调用join以前,先调用close函数,不然会出错。执行完close后不会有新的进程加入到pool,join函数等待全部子进程结束 #循环读出列表返回的结果 for res in result_list: print "the result:",res.get()