Python--day39--管道和数据共享(面试可能会问到)

1,管道

上面所述挂起即为阻塞

管道.py

 1 from multiprocessing import Pipe, Process
 2 
 3 
 4 def func(conn1,conn2):
 5     conn2.close()
 6     while True:
 7         try:
 8             msg = conn1.recv()
 9             print(msg)
10         except EOFError:
11             conn1.close()
12             break
13 
14 if __name__ == '__main__':
15     conn1,conn2 = Pipe()
16     Process(target=func,args =(conn1,conn2)).start()
17     conn1.close()
18     for i in range(20):
19         conn2.send('吃了么')
20     # conn2.send(None)
21     conn2.close()

 2,#pipe 数据不安全性

加锁来控制操作管道的行为 来避免进程之间争抢数据造成的数据不安全现象

总结:

posted @ 2019-01-29 19:41  莱茵河的雨季  阅读(257)  评论(0编辑  收藏  举报