nanomsg-pynng库的简单学习笔记
生活随笔
收集整理的這篇文章主要介紹了
nanomsg-pynng库的简单学习笔记
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
'''
'''
'''
About pynng (python nanomsg next generation
可用的協議有以下幾個:
Pair0
Pair1
Req0 / Rep0
Pub0 / Sub0
Push0 / Pull0
Surveyor0 / Respondent0
Bus0
與另外的一個socket的交流:
dial() --send()
listen() --recv()
#同樣也可以異步 支持的庫是:syncio and Trio.
asend()
arecv()
#最終執行完的時候:close()
'''
'''
實際在開始執行listen() dial() 的時候,我們應該初始化socket,通過傳入一系列的關鍵詞參數
recv_timeout(int):超過固定的時間,報出;pynng.exceptions.Timeout
send_timeout(int):
recv_max_size(int):最大massage的接收情況,默認大小是1MB,如果是0的情況,就是表示不限制接收包的大小
recv_buffer_size(int):socket能夠接收的message的數量
send_buffer_size(int):socket放在buffer中的message的數量
name(str):socket 的name
raw(bool): 決定這個socket是否是raw ,當前只支持cooked,目前不支持raw A boolean, indicating whether the socket is raw or cooked. Returns True if the socket is raw, else False. This property is read-only. Corresponds to library option NNG_OPT_RAW. For more information see nng’s documentation. Note that currently, pynng does not support raw mode sockets, but we intend to in the future:
protocol (int): Read-only option which returns the 16-bit number of the socket’s protocol.
protocol_name (str): Read-only option which returns the name of the socket’s protocol.
peer (int): Returns the peer protocol id for the socket.
local_address: The SockAddr representing the local address.
reconnect_time_min(min):以ms為單位,等待最短的時間去嘗試重新連接
reconnect_time_max(int):在執行連接之前等待的最大的時間的長短he maximum time to wait before attempting reconnects, in ms. Corresponds to NNG_OPT_RECONNMAXT. If this is non-zero, then the time between successive connection attempts will start at the value of reconnect_time_min, and grow exponentially, until it reaches this value. This option can be set on the socket, or on the dialers associated with the socket.
recv_fd(int): 接收文件描述,這個可以被傳入到 select.poll() or select.select() ,否則沒有別的用處
send_fd(int):發送的文件描述
#
# When used in select.poll() or select.select(), recv_fd and send_fd are
# both marked as readable when they can receive or send data without blocking.
# So the upshot is that for select.select()
# they should be passed in as the rlist and for select.poll.register()
# the eventmask should be POLLIN.
'''
'''
幾個函數的簡介:異步函數
await arecv()
await arecv_msg()
await asend(data)
dial(address, *, block=None):block:True:阻塞dial 是可以被嘗試1:If True, a blocking dial is attempted. If it fails for any reason, the dial fails and an exception is raised.2:If False, a non-blocking dial is started. The dial is retried periodically in the background until it is successful.3:(Default behavior): If None, a blocking dial is first attempted. If it fails an exception is logged (using the Python logging module), then a non-blocking dial is done.
listen(address,flags=0):listen at specified address
new_context(): 返回一個新的context給 socket
recv(block=True):在socket上面接收數據,這實際上是一個同步的函數,block默認情況下是True,這個時候會指導接收到數據,否則一直處在阻塞狀態。如果block=False 的情況下, 他會立即返回當前的結果。如果沒有數據就返回pynng.TryAgain
recv_msg(block=True):Receive a Message on the socket.
send(data):Sends data (either bytes or bytearray) on socket.'''
'''
可支持的協議:
1、class pynng.Pair0(**kwargs):::::::::有同步的,有異步的,API,這種情況適用于雙向一對一通訊得情況
2、class pynng.Pair1(*, polyamorous=None, **kwargs):::::::::::這是socket,用于有很多partners得雙向交流得情況
3、:問答:class pynng.Req0(*, resend_time=None, **kwargs)class pynng.Rep0(**kwargs)
4、Pub and Subclass pynng.Pub0(**kwargs)class pynng.Sub0(**kwargs)pub:不能使用recv 否則會提示 pynng.NotSupported exceptionsub:接收同樣的socket的參數,但是還有一個額外的參數就是topics,這個topics 是str bytes類型,這個實際上是一個對應,用來看pub發送的是不是匹配到我當前想要的東西如果這個參數設定為b''空,這個時候,我們就能接收所有的數據了# pub/sub is a “best effort” transport;# if you have a very high volume of messages be prepared for some messages to be silently dropped.5、Push and Pullclass pynng.Push0(**kwargs)class pynng.Pull0(**kwargs)Push: Push0套接字是數據管道的推入端,數據被推送到唯一的一個pull端,這個對于將工作分配給很多節點很好這也就說明了在push端執行recv()是會報錯的Pull:A Pull0 is the receiving end of a data pipeline. It needs to be paired with a Push0 socket. Attempting to send() with a Pull0 socket will raise a pynng.NotSupported exception.
6、 Surveyor0 / Respondent0 class pynng.Surveyor0(**kwargs)class pynng.Respondent0(**kwargs)Surveyor0: 發布一個調查,給這些所有的respodents 一個機會能夠發言Respondent0: 接收到一個消息之后可以發言了,但是你是不能夠提前發言的7、Bus0class pynng.Bus0(**kwargs)Bus0:發送一個msg 給所有直接相聯的peers,這個也就允許我們設計一個網格網絡,這個msg也就僅僅發送給直接相聯的peersYou must explicitly connect all nodes with the listen() and corresponding listen() calls.''''''
一些基本的概念:
1、pipe: There is no public constructor for a Pipe; they are automatically added to the underlying socket whenever the pipe is created.class pynng.Pipe(...)await asend(data)Asynchronously send bytes from this Pipe.send(data):Synchronously send bytes from this Pipe. This method automatically creates a Message,associates with this pipe, and sends it with this pipe’s associated Socket.
2、Contextclass pynng.Context(.....)說明:這個是上下文環境nng_context ,可以通過Socket.new_context() 去創建一個上下文這個context實際上支隊Req0 和 Rep0 才有用,其他的協議是不支持的說明2:如果我們有了上下文環境,我們可以直接使用send() recv() or async equivalent 說明3:這個上下文環境,跟蹤一個協議的狀態,這個上下文環境允許相同的socket用于多種不同的操作---多線程,多協程說明4:上下文環境允許多路復用同一個socket , 它刪除了需要使用原始套接字的最大用例之一。說明5:上下文環境不能直接實例化,我們需要創建一個socket 然后 調用new_context()await arecv()Asynchronously receive data using this context.await arecv_msg()Asynchronously receive a Message on the context.await asend(data)Asynchronously send data using this context.close()Close this context.recv()Synchronously receive data on this context.recv_msg()Synchronously receive a Message using this context.send(data)Synchronously send data on the context.
3、Messageclass pynng.Message(data)說明:使用消息接口可以更好地控制發送消息的各個方面。特別是,您可以判斷消息來自on receive的哪個管道,并且可以指示消息將從on send發送到哪個管道說明1:通常情況,不需要創建Message,只需要通過Socket.recv_msg() 這個時候就實例化了一個message這個我們也就可以通過Pipe.send()來發送數據了說明2:由于我們使用message的情況下,就是為了更加方便使用一個特定pipe所以我們需要能夠更加方便: pipe.send() or pipe.asend()說明3:Messages in pynng are immutable; this is to prevent data corruption.警告:可以使用_buffer屬性訪問消息的底層數據緩沖區。但是,必須注意不要在對緩沖區的引用仍然存在時發送消息;如果在消息發送后使用緩沖區,可能會導致分段錯誤或數據損壞(讀:will)。
4、Dialer class pynng.Dialer(...)說明: A Dialer is associated with a single Socket. The associated socket can be accessed via the socket attribute. There is no public constructor for creating a Dialerclose() 關閉當前dialer
5、Listenerclass pynng.Listener(...)The Python version of nng_listener. A Listener is returned whenever Socket.listen() is called.A list of active listeners can be accessed via Socket.listenersclose()Close the listener.
'''
上面是我過python的腳本寫的,下面是實際的文字:
''' ''' ''' About pynng (python nanomsg next generation 可用的協議有以下幾個: Pair0 Pair1 Req0 / Rep0 Pub0 / Sub0 Push0 / Pull0 Surveyor0 / Respondent0 Bus0 與另外的一個socket的交流: dial() --send() listen() --recv() #同樣也可以異步 支持的庫是:syncio and Trio. asend() arecv() #最終執行完的時候:close() ''' ''' 實際在開始執行listen() dial() 的時候,我們應該初始化socket,通過傳入一系列的關鍵詞參數 recv_timeout(int):超過固定的時間,報出;pynng.exceptions.Timeout send_timeout(int): recv_max_size(int):最大massage的接收情況,默認大小是1MB,如果是0的情況,就是表示不限制接收包的大小 recv_buffer_size(int):socket能夠接收的message的數量 send_buffer_size(int):socket放在buffer中的message的數量 name(str):socket 的name raw(bool): 決定這個socket是否是raw ,當前只支持cooked,目前不支持raw A boolean, indicating whether the socket is raw or cooked. Returns True if the socket is raw, else False. This property is read-only. Corresponds to library option NNG_OPT_RAW. For more information see nng’s documentation. Note that currently, pynng does not support raw mode sockets, but we intend to in the future: protocol (int): Read-only option which returns the 16-bit number of the socket’s protocol. protocol_name (str): Read-only option which returns the name of the socket’s protocol. peer (int): Returns the peer protocol id for the socket. local_address: The SockAddr representing the local address. reconnect_time_min(min):以ms為單位,等待最短的時間去嘗試重新連接 reconnect_time_max(int):在執行連接之前等待的最大的時間的長短he maximum time to wait before attempting reconnects, in ms. Corresponds to NNG_OPT_RECONNMAXT. If this is non-zero, then the time between successive connection attempts will start at the value of reconnect_time_min, and grow exponentially, until it reaches this value. This option can be set on the socket, or on the dialers associated with the socket. recv_fd(int): 接收文件描述,這個可以被傳入到 select.poll() or select.select() ,否則沒有別的用處 send_fd(int):發送的文件描述 # # When used in select.poll() or select.select(), recv_fd and send_fd are # both marked as readable when they can receive or send data without blocking. # So the upshot is that for select.select() # they should be passed in as the rlist and for select.poll.register() # the eventmask should be POLLIN. ''' ''' 幾個函數的簡介:異步函數 await arecv() await arecv_msg() await asend(data) dial(address, *, block=None):block:True:阻塞dial 是可以被嘗試1:If True, a blocking dial is attempted. If it fails for any reason, the dial fails and an exception is raised.2:If False, a non-blocking dial is started. The dial is retried periodically in the background until it is successful.3:(Default behavior): If None, a blocking dial is first attempted. If it fails an exception is logged (using the Python logging module), then a non-blocking dial is done. listen(address,flags=0):listen at specified address new_context(): 返回一個新的context給 socket recv(block=True):在socket上面接收數據,這實際上是一個同步的函數,block默認情況下是True,這個時候會指導接收到數據,否則一直處在阻塞狀態。如果block=False 的情況下, 他會立即返回當前的結果。如果沒有數據就返回pynng.TryAgain recv_msg(block=True):Receive a Message on the socket. send(data):Sends data (either bytes or bytearray) on socket.''' ''' 可支持的協議: 1、class pynng.Pair0(**kwargs):::::::::有同步的,有異步的,API,這種情況適用于雙向一對一通訊得情況 2、class pynng.Pair1(*, polyamorous=None, **kwargs):::::::::::這是socket,用于有很多partners得雙向交流得情況 3、:問答:class pynng.Req0(*, resend_time=None, **kwargs)class pynng.Rep0(**kwargs) 4、Pub and Subclass pynng.Pub0(**kwargs)class pynng.Sub0(**kwargs)pub:不能使用recv 否則會提示 pynng.NotSupported exceptionsub:接收同樣的socket的參數,但是還有一個額外的參數就是topics,這個topics 是str bytes類型,這個實際上是一個對應,用來看pub發送的是不是匹配到我當前想要的東西如果這個參數設定為b''空,這個時候,我們就能接收所有的數據了# pub/sub is a “best effort” transport;# if you have a very high volume of messages be prepared for some messages to be silently dropped.5、Push and Pullclass pynng.Push0(**kwargs)class pynng.Pull0(**kwargs)Push: Push0套接字是數據管道的推入端,數據被推送到唯一的一個pull端,這個對于將工作分配給很多節點很好這也就說明了在push端執行recv()是會報錯的Pull:A Pull0 is the receiving end of a data pipeline. It needs to be paired with a Push0 socket. Attempting to send() with a Pull0 socket will raise a pynng.NotSupported exception. 6、 Surveyor0 / Respondent0 class pynng.Surveyor0(**kwargs)class pynng.Respondent0(**kwargs)Surveyor0: 發布一個調查,給這些所有的respodents 一個機會能夠發言Respondent0: 接收到一個消息之后可以發言了,但是你是不能夠提前發言的7、Bus0class pynng.Bus0(**kwargs)Bus0:發送一個msg 給所有直接相聯的peers,這個也就允許我們設計一個網格網絡,這個msg也就僅僅發送給直接相聯的peersYou must explicitly connect all nodes with the listen() and corresponding listen() calls.'''''' 一些基本的概念: 1、pipe: There is no public constructor for a Pipe; they are automatically added to the underlying socket whenever the pipe is created.class pynng.Pipe(...)await asend(data)Asynchronously send bytes from this Pipe.send(data):Synchronously send bytes from this Pipe. This method automatically creates a Message,associates with this pipe, and sends it with this pipe’s associated Socket. 2、Contextclass pynng.Context(.....)說明:這個是上下文環境nng_context ,可以通過Socket.new_context() 去創建一個上下文這個context實際上支隊Req0 和 Rep0 才有用,其他的協議是不支持的說明2:如果我們有了上下文環境,我們可以直接使用send() recv() or async equivalent 說明3:這個上下文環境,跟蹤一個協議的狀態,這個上下文環境允許相同的socket用于多種不同的操作---多線程,多協程說明4:上下文環境允許多路復用同一個socket , 它刪除了需要使用原始套接字的最大用例之一。說明5:上下文環境不能直接實例化,我們需要創建一個socket 然后 調用new_context()await arecv()Asynchronously receive data using this context.await arecv_msg()Asynchronously receive a Message on the context.await asend(data)Asynchronously send data using this context.close()Close this context.recv()Synchronously receive data on this context.recv_msg()Synchronously receive a Message using this context.send(data)Synchronously send data on the context. 3、Messageclass pynng.Message(data)說明:使用消息接口可以更好地控制發送消息的各個方面。特別是,您可以判斷消息來自on receive的哪個管道,并且可以指示消息將從on send發送到哪個管道說明1:通常情況,不需要創建Message,只需要通過Socket.recv_msg() 這個時候就實例化了一個message這個我們也就可以通過Pipe.send()來發送數據了說明2:由于我們使用message的情況下,就是為了更加方便使用一個特定pipe所以我們需要能夠更加方便: pipe.send() or pipe.asend()說明3:Messages in pynng are immutable; this is to prevent data corruption.警告:可以使用_buffer屬性訪問消息的底層數據緩沖區。但是,必須注意不要在對緩沖區的引用仍然存在時發送消息;如果在消息發送后使用緩沖區,可能會導致分段錯誤或數據損壞(讀:will)。 4、Dialer class pynng.Dialer(...)說明: A Dialer is associated with a single Socket. The associated socket can be accessed via the socket attribute. There is no public constructor for creating a Dialerclose() 關閉當前dialer 5、Listenerclass pynng.Listener(...)The Python version of nng_listener. A Listener is returned whenever Socket.listen() is called.A list of active listeners can be accessed via Socket.listenersclose()Close the listener. '''總結
以上是生活随笔為你收集整理的nanomsg-pynng库的简单学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 词条创建
- 下一篇: 易语言之后,中文编程该何去何从?新式中文