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

相关文章
|
23小时前
|
消息中间件 Unix Linux
【探索Linux】P.14(进程间通信 | 匿名管道 | |进程池 | pipe() 函数 | mkfifo() 函数)
【探索Linux】P.14(进程间通信 | 匿名管道 | |进程池 | pipe() 函数 | mkfifo() 函数)
22 0
|
23小时前
|
监控 Python
python过滤指定进程
python过滤指定进程
15 1
|
23小时前
|
运维 监控 Ubuntu
Python实现ubuntu系统进程内存监控
Python实现ubuntu系统进程内存监控
16 1
|
23小时前
|
开发者 Python
在Python中查询进程信息的实用指南
在Python中查询进程信息的实用指南
10 2
|
23小时前
|
消息中间件 Linux 调度
Python的进程锁,进程队列
Python的进程锁,进程队列
123 3
|
23小时前
|
数据采集 监控 调度
Python的进程,以及进程同步,守护进程详细解读
Python的进程,以及进程同步,守护进程详细解读
140 4
|
23小时前
|
调度 Python 容器
【python】-详解进程与线程
【python】-详解进程与线程
|
23小时前
|
运维 监控 Unix
第十五章 Python多进程与多线程
第十五章 Python多进程与多线程
|
23小时前
|
Java 数据库连接 数据处理
Python从入门到精通:3.1.2多线程与多进程编程
Python从入门到精通:3.1.2多线程与多进程编程
|
23小时前
|
消息中间件 安全 调度
Python从入门到精通:3.1.1多线程与多进程——进程和线程的概念
Python从入门到精通:3.1.1多线程与多进程——进程和线程的概念