day41-进程-管道

#1、管道Pipe:双向通信:
from multiprocessing import Pipe
p1,p2 = Pipe()
p1.send('hello')
print(p2.recv())
p2.send('hi')
print(p1.recv())
# hello
# hi

#2、当管道一端关闭,并且管道没有值的时候,接收就会报错:
from multiprocessing import Pipe
p1,p2 = Pipe()
p1.send('hello')
p1.close() #管道一边关闭。
print(p2.recv()) #hello
print(p2.recv()) #EOFError报错。当管道一端关闭,并且管道没有值的时候,接收就会报错。

#3、主进程发送数据给子进程:
from multiprocessing import Pipe
from multiprocessing import Process
def func(son):
    print(son.recv())

if __name__ == '__main__':
    foo,son = Pipe()
    p = Process(target=func,args=(son,))
    p.start()
    foo.send('hello')
# hello

#队列 = 管道+锁,队列的数据是安全的,管道因为没有锁,数据是不安全的。
#进程之间的通信(IPC):队列和管道。

猜你喜欢

转载自www.cnblogs.com/python-daxiong/p/12142749.html