python进程池调用实例方法_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,[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 进程池使用之坑~~

# -*- 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])

执行结果:

[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()

©著作权归作者所有:来自51CTO博客作者vekergu的原创作品,如需转载,请注明出处,否则将追究法律责任

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值