您的位置:首页 > 编程语言 > Python开发

11.python并发入门(part10 多进程之间实现通信,以及进程之间的数据共享)

2017-05-15 16:12 1006 查看
一、进程队列。

多个进程去操作一个队列中的数据,外观上看起来一个进程队列,只是一个队列而已,单实际上,你开了多少个进程,这些进程一旦去使用这个队列,那么这个队列就会被复制多少份。
(队列=管道+锁)
这么做的主要原因就是,不同进程之间的数据是无法共享的。
下面是使用进程队列使多进程之间互相通信的示例:
下面这个例子,就是往进程队列里面put内容。
#!/usr/local/bin/python2.7
# -*- coding:utf-8 -*-
import multiprocessing
def func1(que,n):
que.put(n+1)
print "son process queue id is %s:" %(id(que))
if __name__ == '__main__':
q1 = multiprocessing.Queue() #生成一个进程队列!!!!这个队列和普通队列是不同的!!
print "main process queue id is %s:" %(id(q1))
for i in range(3):
p = multiprocessing.Process(target=func1,args=(q1,i))
p.start()
print q1.get()
print q1.get()
print q1.get()

输出结果:

#windows和类unix系统运行的结果是不一样的!!在windows系统下,进程队列的id号是不同的!!
main process queue id is 4459325136:
son process queue id is 4459325136:
1
son process queue id is 4459325136:
2
son process queue id is 4459325136:
3

二、通过管道来实现进程间通信。
下面是使用管道的方式让两个进程通信的示例。

下面这段代码主要实现了父进程给子进程互相发送消息。
#!/usr/local/bin/python2.7
# -*- coding:utf-8 -*-
import multiprocessing
def func1(conn):
conn.send("hi daddy!") #子进程给父进程发送内容。
response = conn.recv() #子进程等待接收父进程发来的内容。
print "respnse:%s" %(response)
conn.close()
print "child conn id %s" %(id(child_conn))
if __name__ == '__main__':
parent_conn,child_conn = multiprocessing.Pipe() #使用multiprocessing的pipe类来创建出两个双向的管道对象,注意哦,这两个管道都是双向的,可以收也可以发。
print "main child conn: %s" %(id(child_conn))
p = multiprocessing.Process(target=func1,args=(child_conn,))
p.start()
print parent_conn.recv() #父进程等待接收子进程的内容。
parent_conn.send("hi child!") #父进程给子进程发送内容。
p.join()
使用pipe类生成的两个管道相当于两部电话,一个给了父进程,另一个给了子进程。
官方说法如下:

The two connection objects returned by Pipe() represent the two ends of the pipe. Each connection object has send() and recv() methods (among others). Note that data in a pipe may become corrupted if two processes (or threads) try to read from or write to the same end of the pipe at the same time. Of course there is no risk of corruption from processes using different ends of the pipe at the same time.
pipe对象会创建出两个链接对象(管道),每个链接对象(管道)的两端,都可以send(发送),recv(接收)等。
不过需要注意的是!两个进程或者线程同时,去读取或者写入管道的同一端,那么数据很有可能会损坏!!

总结下进程队列和管道之间的关系:
管道和进程队列,实现的都是两个子进程之间,以及父进程和子进程之间的通信功能,它们做的都是同一件事,只不过是实现的方法不一样,它们完成的都只是进程和进程之间的通信,注意!!只是数据通信!!而不是数据共享!这个概念不要弄混了!
那什么是真正的数据共享?
当一个进程去修改某个数据,另外一个进程内部也会产生变化,这才是真正意义上在多进程之间实现的数据共享。

在举一个比较好理解的例子,假如一个列表里面有三个元素,进程1去这个列表中删除一个元素,进程2再去print这个列表时,这个列表里只剩下了两个元素。

那么如何去实现进程间的数据共享?这就要由Manager去实现了。

三、使用Manager来实现进程和进程间的数据共享。
队列和管道只是实现了数据交互,并没实现数据共享,共享就是一个进程去更改另一个进程的数据。
官方对于Managers的介绍:

A manager object returned by Manager() controls a server process which holds Python objects and allows other processes to manipulate them using proxies.
Manager()会返回一个 Manager对象,它控制一个python服务进程,并允许其他进程使用代理的方式来操作它们

下面是manger对象都支持哪些数据类型?
A manager returned by Manager() will support types list, dict, Namespace, Lock, RLock, Semaphore, BoundedSemaphore, Condition, Event, Barrier, Queue, Value and Array. For example:

列表,字典,名称空间(变量),锁,递归锁,信号量,条件变量,事件,队列...等...因为有些类型博主也没有接触过......

下面是个进程间实现数据共享的例子:
#!/usr/local/bin/python2.7
# -*- coding:utf-8 -*-
import multiprocessing
def func1(dic_1,list_1,name):
dic_1[name] = 'test'
#dic_1['num'] = 123
list_1.append(name)
print "son process dict id:%s list id :%s " %(id(dic_1),id(list_1))
if __name__ == '__main__':
with multiprocessing.Manager() as manager:
d1 = manager.dict() #注意这里!!如果字典和列表等数据类型,想要被进程之间共享,必须要使用manager下面封装好的类!!
l1 = manager.list() #这里使用的也是manager中已经封装好的特殊list!
print "main process dict id:%s list id :%s " %(id(d1),id(l1))
pro_list = []
for i in range(10):
p = multiprocessing.Process(target=func1,args=(d1,l1,str(i),))
p.start()
pro_list.append(p)
for res in pro_list:
res.join()
print d1
print l1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息