38. Python 多进程Manager 进程池

强大的Manager模块

上一节实现的数据共享的方式只有两种结构Value和Array。

Python中提供了强大的Manager模块,专门用来做数据共享。

他支持的类型非常多,包括:Value、Araay、list、dict、Queue、Lock等。

以下例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import  multiprocessing
def  worker(d,l):
     + =  range ( 11 16 )
     for  in  xrange ( 1 6 ):
         key  =  "key{0}" . format (i)
         val  =  "val{0}" . format (i)
         d[key]  =  val
 
if  __name__  = =  "__main__" :
     manager  =  multiprocessing.Manager()
     =  manager. dict ()
     =  manager. list ()
     =  multiprocessing.Process(target = worker, args = (d, l))
     p.start()
     p.join()
     print (d)
     print (l)

打印结果:

1
2
{ 'key3' 'val3' 'key2' 'val2' 'key1' 'val1' 'key5' 'val5' 'key4' 'val4' }
[11, 12, 13, 14, 15]



进程池:

Pool可以提供指定数量的进程,供用户调用,当有新的请求提交到pool中时,

如果池还没有满,那么就会创建一个新的进程用来执行该请求;

但如果池中的进程数已经达到规定最大值,那么该请求就会等待,直到池中有进程结束,才会创建新的进程。

阻塞和非阻塞的区别

Pool.apply_async     非阻塞,定义的进程池进程最大数可以同时执行。

Pool.apply            一个进程结束,释放回进程池,下一个进程才可以开始

举例:

非阻塞:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import  multiprocessing
import  time
def  worker(msg):
     print  ( "#######start {0}########" . format (msg))
     time.sleep( 1 )
     print  ( "#######end   {0}########" . format (msg))
 
if  __name__  = =  "__main__" :
     pool  =  multiprocessing.Pool(processes = 3 )
     for  in  xrange ( 1 10 ):
         msg  =  "hello{0}" . format (i)
         pool.apply_async(func = worker, args = (msg,))
     pool.close()
     pool.join()      #调用join之前,先调用close函数,否则会出错。执行完close后不会有新的进程加入到pool,join函数等待所有子进程结束
     print  ( "main end" )

打印结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#######start hello1########
#######start hello2########
#######start hello3########
#######end   hello1########
#######start hello4########
#######end   hello2########
#######start hello5########
#######end   hello3########
#######start hello6########
#######end   hello4########
#######start hello7########
#######end   hello5########
#######start hello8########
#######end   hello6########
#######start hello9########
#######end   hello7########
#######end   hello8########
#######end   hello9########
main end



阻塞:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import  multiprocessing
import  time
def  worker(msg):
     print  ( "#######start {0}########" . format (msg))
     time.sleep( 1 )
     print  ( "#######end   {0}########" . format (msg))
     
if  __name__  = =  "__main__" :
     pool  =  multiprocessing.Pool(processes = 3 )
     for  in  xrange ( 1 10 ):
         msg  =  "hello{0}" . format (i)
         pool. apply (func = worker, args = (msg,))
     pool.close()
     pool.join()      #调用join之前,先调用close函数,否则会出错。执行完close后不会有新的进程加入到pool,join函数等待所有子进程结束
     print  ( "main end" )

打印结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#######start hello1########
#######end   hello1########
#######start hello2########
#######end   hello2########
#######start hello3########
#######end   hello3########
#######start hello4########
#######end   hello4########
#######start hello5########
#######end   hello5########
#######start hello6########
#######end   hello6########
#######start hello7########
#######end   hello7########
#######start hello8########
#######end   hello8########
#######start hello9########
#######end   hello9########
main end


对比一下两种类型的输出状态即可明白。



本文转自 听丶飞鸟说 51CTO博客,原文链接:http://blog.51cto.com/286577399/2049893

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值