我首先是参考了https://pynng.readthedocs.io/en/latest/core.html#pynng.Pipe.send
这个网址,是专门将pynng这个库的,下面的是基于同步的库的
具体的结构图:
#我写的一个代码,大家随便改改随便试试
# from pynng import Pair1
''''
address = 'tcp://127.0.0.1:12343'
with Pair1(listen=address, polyamorous=True) as s0, \
Pair1(dial=address, polyamorous=True) as s1, \
Pair1(dial=address, polyamorous=True) as s2:
s1.send(b'hello from s1') ####@@@@@s1 发出hello from s1
s2.send(b'hello from s2') #@ s2 发出hello from s2
msg1 = s0.recv_msg() #@s0去接收信息
msg2 = s0.recv_msg() #@ s0 再去接收
print(msg1.bytes) # prints b'hello from s1' #@先得到s1
print(msg2.bytes) # prints b'hello from s2' #@ 再得到s2得发来得信息
msg1.pipe.send(b'hey s1') # @从接收得管道中发送
msg2.pipe.send(b'hey s2') #@ 从该接收得管道中发送msg
print(s2.recv()) # prints b'hey s2' #@ 看到底发送接收到了没有
print(s1.recv()) # prints b'hey s1' #@ 看s1 是否接收到了
'''
'''
下面一部分,来进行我对上面pair1 的一个修改
'''
from pynng import Pair1
address = 'tcp://127.0.0.1:12343'
with Pair1(listen=address, polyamorous=True) as s0, \
Pair1(dial=address, polyamorous=True) as s1, \
Pair1(dial=address, polyamorous=True) as s2:
s2.send(b'hello from s2') # @ s2 发出hello from s2
s1.send(b'hello from s1') ####@@@@@s1 发出hello from s1
msg1 = s0.recv_msg() #@s0去接收信息
msg2 = s0.recv_msg() #@ s0 再去接收
# s1.send(b'for test')
# msg4 = s2.recv_msg()
# msg4.pipe.send(b's1,s2connect to each other')
# print(type(msg2)) #这个是pnng的msg的类型,这个类型存在pipe 管道的说法可以到之后,也就表示他们之间建立了连接咯
# msg3=s0.recv() # 而这个recv 接收到的就是不同的字节流的类型的数据
# print(msg3)
# print(type(msg3))
print(msg1.bytes) # prints b'hello from s1' #@先得到s1
print(msg2.bytes) # prints b'hello from s2' #@ 再得到s2得发来得信息
# msg1.pipe.send(b'hey s1') # @从接收得管道中发送
#@ 从该接收得管道中发送msg
# msg2.pipe.send(b'hey s2')
# msg3=s1.recv_msg()
# msg4=s2.recv_msg()
"""
s1.send(b's1 again')
s1.send(b's1 again again')
s1.send(b's1 again again again')
s2.send(b's2 again')
s2.send(b's2 again again ')
# s2.send(b's2 again again again')
print(s0.recv())
print(s0.recv())
print(s0.recv())
print(s0.recv())
# print(s0.recv())
# print(s0.recv())
# print(s0.recv())
"""
s0.send(b'i am s0')
s0.send(b'i am s0')
s0.send(b'i am s0')
print(s1.recv())
# print(s2.recv())
# print(msg3.bytes)
# print(msg4.bytes)
#
# msg1.pipe.send(b'let us talk ok?')
# msg3.pipe.send(b'we can talk')
# msg2.pipe.send(b'')
# print(s0.recv())
# print(s1.recv())
# print(s2.recv()) # prints b'hey s2' #@ 看到底发送接收到了没有
# print(s1.recv()) # prints b'hey s1' #@ 看s1 是否接收到了
# print(s1.recv())
# s0.send(b'send to who')
# print(s2.recv())
# s1.send(b'testagain')
# print(s0.recv())
# s0.send(b'hello1')
# print(s1.recv())
# s0.send(b'hello2')
# print(s2.recv())
###我们测试了一下得到了结论
'''
s1 发出
s2 发出
msg1 s0接收
msg2 s0接收
实际上,这样也就建立了s1 与 s0 s2 与s0 之间的连接了
情况1:
但是是基于msg1是s0接受了来自s1 msg2是s0接收了来自s2 这种实例的对象的哦 实际上,我们就可以认为这是个管道 这个时候建立了管道
s1 s2反正就一直发送
我们这个时候就用s0 一直接收试试
s1.send(b's1 again')
s1.send(b's1 again again')
s1.send(b's1 again again again')
s2.send(b's2 again')
s2.send(b's2 again again ')
# s2.send(b's2 again again again')
print(s0.recv())
print(s0.recv())
print(s0.recv())
print(s0.recv())
测试结果:交替接收
b's1 again'
b's1 again again'
b's2 again'
b's2 again again '
如果我们再建立msg3是s1接收来自s0的实际的对象 msg4是s2接收来自s0的实例 的情况,这个时候,
我们就可以分别使用msg3 和 msg1 对应的实际的pipe 互相发送和接收详细了,我认为这个应当是实际上建立了两个管道的情况,不会造成影响
'''
大家也可以自己去尝试各种的修改,我当前的认知就是:
每一个节点通过一个pipe建立一个连接,这个连接实际上是一个双向的呢?
建立的msg的对象可以通过msg.pipe.send()进行向绑定的s1 发送,而s1的发送也就直接使用:我们看一下执行的结果
通过上面的两个图我们可以看到接收的情况并没有什么具体的顺序
第二部分:s0 直接使用send进行发送,只有s1 能够接收到并不知道为什么
看第二份的测试结果:我们的s0 和s1 s2 通过msg1 msg2 这个实例对象链接了起来