前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 进程池pool简单实例

python 进程池pool简单实例

作者头像
py3study
发布2020-01-03 12:18:25
2.1K0
发布2020-01-03 12:18:25
举报
文章被收录于专栏:python3python3

进程池:

   在利用Python进行系统管理的时候,特别是同时操作多个文件目录,或者远程控制多台主机,并行操作可以节约大量的时间。当被操作对象数目不大时,可以直接利用multiprocessing中的Process动态成生多个进程,十几个还好,但如果是上百个,上千个目标,手动的去限制进程数量却又太过繁琐,此时可以发挥进程池的功效。  

    Pool可以提供指定数量的进程供用户调用,当有新的请求提交到pool中时,如果池还没有满,那么就会创建一个新的进程用来执行该请求;但如果池中的进程数已经达到规定最大值,那么该请求就会等待,直到池中有进程结束,才会创建新的进程来它。

如何使用进程池?

1 如何使用进程池执行函数?

a 不返回参数

代码语言:javascript
复制
# -*- 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结果区别)

2 进程池使用之坑~~

代码语言:javascript
复制
# -*- 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])

执行结果:

代码语言:javascript
复制
[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脚本,后面的脚本却没有输出应有的结果,为什么?

     首先对上列程序进行细微调整:

代码语言:javascript
复制
# -*- 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函数等待所有子进程结束

返回结果:

代码语言:javascript
复制
[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   进程池调用后返回参数

代码语言:javascript
复制
# -*- 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()函数得出每个返回结果的值

执行结果:

代码语言:javascript
复制
[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方法,脚本仍然能正常显示。

    但是为了代码更加稳定,还是建议增加主进程阻塞(除非主进程需要等待子进程返回结果):

代码语言:javascript
复制
# -*- 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()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档