nanomsg----pair1 的介绍
生活随笔
收集整理的這篇文章主要介紹了
nanomsg----pair1 的介绍
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我首先是參考了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 s1s2.send(b'hello from s2') #@ s2 發出hello from s2msg1 = s0.recv_msg() #@s0去接收信息msg2 = s0.recv_msg() #@ s0 再去接收print(msg1.bytes) # prints b'hello from s1' #@先得到s1print(msg2.bytes) # prints b'hello from s2' #@ 再得到s2得發來得信息msg1.pipe.send(b'hey s1') # @從接收得管道中發送msg2.pipe.send(b'hey s2') #@ 從該接收得管道中發送msgprint(s2.recv()) # prints b'hey s2' #@ 看到底發送接收到了沒有print(s1.recv()) # prints b'hey s1' #@ 看s1 是否接收到了 '''''' 下面一部分,來進行我對上面pair1 的一個修改 ''' from pynng import Pair1address = '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 s2s1.send(b'hello from s1') ####@@@@@s1 發出hello from s1msg1 = 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' #@先得到s1print(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 這個實例對象鏈接了起來
?
總結
以上是生活随笔為你收集整理的nanomsg----pair1 的介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SCCM2007R2 setp by s
- 下一篇: S5700交换机MAC地址绑定问题