上帝大脸 发表于 2018-8-3 11:31:56

python 进程池pool简单实例

  进程池:
  在利用Python进行系统管理的时候,特别是同时操作多个文件目录,或者远程控制多台主机,并行操作可以节约大量的时间。当被操作对象数目不大时,可以直接利用multiprocessing中的Process动态成生多个进程,十几个还好,但如果是上百个,上千个目标,手动的去限制进程数量却又太过繁琐,此时可以发挥进程池的功效。
  Pool可以提供指定数量的进程供用户调用,当有新的请求提交到pool中时,如果池还没有满,那么就会创建一个新的进程用来执行该请求;但如果池中的进程数已经达到规定最大值,那么该请求就会等待,直到池中有进程结束,才会创建新的进程来它。
  如何使用进程池?
  1 如何使用进程池执行函数?
  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,)
  

  
执行结果:
  
# 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 进程池使用之坑~~
# -*- 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 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
  
# 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
  
# python pool.py
  
# python pool.py
  
# 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,)
  
p.close()
  
p.join() #调用join之前,先调用close函数,否则会出错。执行完close后不会有新的进程加入到pool,join函数等待所有子进程结束
  返回结果:
# 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
  
# 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
  
# 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,))#将返回结果append到列表中
  

  
#循环读出列表返回的结果
  
for res in result_list:
  
print "the result:",res.get()
  注:get()函数得出每个返回结果的值
  执行结果:
# 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
  
# 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
  
# 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,))#将返回结果append到列表中
  

  
p.close()
  
p.join() #调用join之前,先调用close函数,否则会出错。执行完close后不会有新的进程加入到pool,join函数等待所有子进程结束
  
#循环读出列表返回的结果
  
for res in result_list:
  
print "the result:",res.get()
页: [1]
查看完整版本: python 进程池pool简单实例