设为首页 收藏本站
查看: 964|回复: 0

[经验分享] python 进程池pool简单实例

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-9-17 10:08:05 | 显示全部楼层 |阅读模式
进程池:   
   在利用Python进行系统管理的时候,特别是同时操作多个文件目录,或者远程控制多台主机,并行操作可以节约大量的时间。当被操作对象数目不大时,可以直接利用multiprocessing中的Process动态成生多个进程,十几个还好,但如果是上百个,上千个目标,手动的去限制进程数量却又太过繁琐,此时可以发挥进程池的功效。  
    Pool可以提供指定数量的进程供用户调用,当有新的请求提交到pool中时,如果池还没有满,那么就会创建一个新的进程用来执行该请求;但如果池中的进程数已经达到规定最大值,那么该请求就会等待,直到池中有进程结束,才会创建新的进程来它。
如何使用进程池?
1 如何使用进程池执行函数?
a 不返回参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# -*- 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,)

执行结果:
# 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结果区别)


2 进程池使用之坑~~
1
2
3
4
5
6
7
8
9
10
11
# -*- 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,)



执行结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[iyunv@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
[iyunv@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
[iyunv@python thread]# python pool.py
[iyunv@python thread]# python pool.py
[iyunv@python thread]# python pool.py



     从上面的例子可以看出,我们连续执行pool.py脚本,后面的脚本却没有输出应有的结果,为什么?
     首先对上列程序进行细微调整:
1
2
3
4
5
6
7
8
9
# -*- 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,)
p.close()
p.join() #调用join之前,先调用close函数,否则会出错。执行完close后不会有新的进程加入到pool,join函数等待所有子进程结束



返回结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[iyunv@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
[iyunv@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
[iyunv@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   进程池调用后返回参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# -*- 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,))  #将返回结果append到列表中

#循环读出列表返回的结果
for res in result_list:
  print "the result:",res.get()



  注:get()函数得出每个返回结果的值
执行结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[iyunv@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
[iyunv@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
[iyunv@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方法,脚本仍然能正常显示。
    但是为了代码更加稳定,还是建议增加主进程阻塞(除非主进程需要等待子进程返回结果):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# -*- 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,))  #将返回结果append到列表中

p.close()
p.join() #调用join之前,先调用close函数,否则会出错。执行完close后不会有新的进程加入到pool,join函数等待所有子进程结束
#循环读出列表返回的结果
for res in result_list:
  print "the result:",res.get()



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-114821-1-1.html 上篇帖子: python monkey_patch浅析 下篇帖子: Unison+inotify做双向同步,脚本开机运行但数据没有同步,请高手帮忙指导。 python
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表