nanomsg-pynng库的简单学习笔记
生活随笔
收集整理的這篇文章主要介紹了
nanomsg-pynng库的简单学习笔记
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
'''
'''
'''
About pynng (python nanomsg next generation
可用的協(xié)議有以下幾個(gè):
Pair0
Pair1
Req0 / Rep0
Pub0 / Sub0
Push0 / Pull0
Surveyor0 / Respondent0
Bus0
與另外的一個(gè)socket的交流:
dial() --send()
listen() --recv()
#同樣也可以異步 支持的庫是:syncio and Trio.
asend()
arecv()
#最終執(zhí)行完的時(shí)候:close()
'''
'''
實(shí)際在開始執(zhí)行l(wèi)isten() dial() 的時(shí)候,我們應(yīng)該初始化socket,通過傳入一系列的關(guān)鍵詞參數(shù)
recv_timeout(int):超過固定的時(shí)間,報(bào)出;pynng.exceptions.Timeout
send_timeout(int):
recv_max_size(int):最大massage的接收情況,默認(rèn)大小是1MB,如果是0的情況,就是表示不限制接收包的大小
recv_buffer_size(int):socket能夠接收的message的數(shù)量
send_buffer_size(int):socket放在buffer中的message的數(shù)量
name(str):socket 的name
raw(bool): 決定這個(gè)socket是否是raw ,當(dāng)前只支持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為單位,等待最短的時(shí)間去嘗試重新連接
reconnect_time_max(int):在執(zhí)行連接之前等待的最大的時(shí)間的長短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): 接收文件描述,這個(gè)可以被傳入到 select.poll() or select.select() ,否則沒有別的用處
send_fd(int):發(fā)送的文件描述
#
# 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.
'''
'''
幾個(gè)函數(shù)的簡介:異步函數(shù)
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(): 返回一個(gè)新的context給 socket
recv(block=True):在socket上面接收數(shù)據(jù),這實(shí)際上是一個(gè)同步的函數(shù),block默認(rèn)情況下是True,這個(gè)時(shí)候會(huì)指導(dǎo)接收到數(shù)據(jù),否則一直處在阻塞狀態(tài)。如果block=False 的情況下, 他會(huì)立即返回當(dāng)前的結(jié)果。如果沒有數(shù)據(jù)就返回pynng.TryAgain
recv_msg(block=True):Receive a Message on the socket.
send(data):Sends data (either bytes or bytearray) on socket.'''
'''
可支持的協(xié)議:
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 否則會(huì)提示 pynng.NotSupported exceptionsub:接收同樣的socket的參數(shù),但是還有一個(gè)額外的參數(shù)就是topics,這個(gè)topics 是str bytes類型,這個(gè)實(shí)際上是一個(gè)對應(yīng),用來看pub發(fā)送的是不是匹配到我當(dāng)前想要的東西如果這個(gè)參數(shù)設(shè)定為b''空,這個(gè)時(shí)候,我們就能接收所有的數(shù)據(jù)了# 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套接字是數(shù)據(jù)管道的推入端,數(shù)據(jù)被推送到唯一的一個(gè)pull端,這個(gè)對于將工作分配給很多節(jié)點(diǎn)很好這也就說明了在push端執(zhí)行recv()是會(huì)報(bào)錯(cuò)的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: 發(fā)布一個(gè)調(diào)查,給這些所有的respodents 一個(gè)機(jī)會(huì)能夠發(fā)言Respondent0: 接收到一個(gè)消息之后可以發(fā)言了,但是你是不能夠提前發(fā)言的7、Bus0class pynng.Bus0(**kwargs)Bus0:發(fā)送一個(gè)msg 給所有直接相聯(lián)的peers,這個(gè)也就允許我們設(shè)計(jì)一個(gè)網(wǎng)格網(wǎng)絡(luò),這個(gè)msg也就僅僅發(fā)送給直接相聯(lián)的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(.....)說明:這個(gè)是上下文環(huán)境nng_context ,可以通過Socket.new_context() 去創(chuàng)建一個(gè)上下文這個(gè)context實(shí)際上支隊(duì)Req0 和 Rep0 才有用,其他的協(xié)議是不支持的說明2:如果我們有了上下文環(huán)境,我們可以直接使用send() recv() or async equivalent 說明3:這個(gè)上下文環(huán)境,跟蹤一個(gè)協(xié)議的狀態(tài),這個(gè)上下文環(huán)境允許相同的socket用于多種不同的操作---多線程,多協(xié)程說明4:上下文環(huán)境允許多路復(fù)用同一個(gè)socket , 它刪除了需要使用原始套接字的最大用例之一。說明5:上下文環(huán)境不能直接實(shí)例化,我們需要?jiǎng)?chuàng)建一個(gè)socket 然后 調(diào)用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)說明:使用消息接口可以更好地控制發(fā)送消息的各個(gè)方面。特別是,您可以判斷消息來自on receive的哪個(gè)管道,并且可以指示消息將從on send發(fā)送到哪個(gè)管道說明1:通常情況,不需要?jiǎng)?chuàng)建Message,只需要通過Socket.recv_msg() 這個(gè)時(shí)候就實(shí)例化了一個(gè)message這個(gè)我們也就可以通過Pipe.send()來發(fā)送數(shù)據(jù)了說明2:由于我們使用message的情況下,就是為了更加方便使用一個(gè)特定pipe所以我們需要能夠更加方便: pipe.send() or pipe.asend()說明3:Messages in pynng are immutable; this is to prevent data corruption.警告:可以使用_buffer屬性訪問消息的底層數(shù)據(jù)緩沖區(qū)。但是,必須注意不要在對緩沖區(qū)的引用仍然存在時(shí)發(fā)送消息;如果在消息發(fā)送后使用緩沖區(qū),可能會(huì)導(dǎo)致分段錯(cuò)誤或數(shù)據(jù)損壞(讀: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() 關(guān)閉當(dāng)前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的腳本寫的,下面是實(shí)際的文字:
''' ''' ''' About pynng (python nanomsg next generation 可用的協(xié)議有以下幾個(gè): Pair0 Pair1 Req0 / Rep0 Pub0 / Sub0 Push0 / Pull0 Surveyor0 / Respondent0 Bus0 與另外的一個(gè)socket的交流: dial() --send() listen() --recv() #同樣也可以異步 支持的庫是:syncio and Trio. asend() arecv() #最終執(zhí)行完的時(shí)候:close() ''' ''' 實(shí)際在開始執(zhí)行l(wèi)isten() dial() 的時(shí)候,我們應(yīng)該初始化socket,通過傳入一系列的關(guān)鍵詞參數(shù) recv_timeout(int):超過固定的時(shí)間,報(bào)出;pynng.exceptions.Timeout send_timeout(int): recv_max_size(int):最大massage的接收情況,默認(rèn)大小是1MB,如果是0的情況,就是表示不限制接收包的大小 recv_buffer_size(int):socket能夠接收的message的數(shù)量 send_buffer_size(int):socket放在buffer中的message的數(shù)量 name(str):socket 的name raw(bool): 決定這個(gè)socket是否是raw ,當(dāng)前只支持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為單位,等待最短的時(shí)間去嘗試重新連接 reconnect_time_max(int):在執(zhí)行連接之前等待的最大的時(shí)間的長短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): 接收文件描述,這個(gè)可以被傳入到 select.poll() or select.select() ,否則沒有別的用處 send_fd(int):發(fā)送的文件描述 # # 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. ''' ''' 幾個(gè)函數(shù)的簡介:異步函數(shù) 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(): 返回一個(gè)新的context給 socket recv(block=True):在socket上面接收數(shù)據(jù),這實(shí)際上是一個(gè)同步的函數(shù),block默認(rèn)情況下是True,這個(gè)時(shí)候會(huì)指導(dǎo)接收到數(shù)據(jù),否則一直處在阻塞狀態(tài)。如果block=False 的情況下, 他會(huì)立即返回當(dāng)前的結(jié)果。如果沒有數(shù)據(jù)就返回pynng.TryAgain recv_msg(block=True):Receive a Message on the socket. send(data):Sends data (either bytes or bytearray) on socket.''' ''' 可支持的協(xié)議: 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 否則會(huì)提示 pynng.NotSupported exceptionsub:接收同樣的socket的參數(shù),但是還有一個(gè)額外的參數(shù)就是topics,這個(gè)topics 是str bytes類型,這個(gè)實(shí)際上是一個(gè)對應(yīng),用來看pub發(fā)送的是不是匹配到我當(dāng)前想要的東西如果這個(gè)參數(shù)設(shè)定為b''空,這個(gè)時(shí)候,我們就能接收所有的數(shù)據(jù)了# 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套接字是數(shù)據(jù)管道的推入端,數(shù)據(jù)被推送到唯一的一個(gè)pull端,這個(gè)對于將工作分配給很多節(jié)點(diǎn)很好這也就說明了在push端執(zhí)行recv()是會(huì)報(bào)錯(cuò)的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: 發(fā)布一個(gè)調(diào)查,給這些所有的respodents 一個(gè)機(jī)會(huì)能夠發(fā)言Respondent0: 接收到一個(gè)消息之后可以發(fā)言了,但是你是不能夠提前發(fā)言的7、Bus0class pynng.Bus0(**kwargs)Bus0:發(fā)送一個(gè)msg 給所有直接相聯(lián)的peers,這個(gè)也就允許我們設(shè)計(jì)一個(gè)網(wǎng)格網(wǎng)絡(luò),這個(gè)msg也就僅僅發(fā)送給直接相聯(lián)的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(.....)說明:這個(gè)是上下文環(huán)境nng_context ,可以通過Socket.new_context() 去創(chuàng)建一個(gè)上下文這個(gè)context實(shí)際上支隊(duì)Req0 和 Rep0 才有用,其他的協(xié)議是不支持的說明2:如果我們有了上下文環(huán)境,我們可以直接使用send() recv() or async equivalent 說明3:這個(gè)上下文環(huán)境,跟蹤一個(gè)協(xié)議的狀態(tài),這個(gè)上下文環(huán)境允許相同的socket用于多種不同的操作---多線程,多協(xié)程說明4:上下文環(huán)境允許多路復(fù)用同一個(gè)socket , 它刪除了需要使用原始套接字的最大用例之一。說明5:上下文環(huán)境不能直接實(shí)例化,我們需要?jiǎng)?chuàng)建一個(gè)socket 然后 調(diào)用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)說明:使用消息接口可以更好地控制發(fā)送消息的各個(gè)方面。特別是,您可以判斷消息來自on receive的哪個(gè)管道,并且可以指示消息將從on send發(fā)送到哪個(gè)管道說明1:通常情況,不需要?jiǎng)?chuàng)建Message,只需要通過Socket.recv_msg() 這個(gè)時(shí)候就實(shí)例化了一個(gè)message這個(gè)我們也就可以通過Pipe.send()來發(fā)送數(shù)據(jù)了說明2:由于我們使用message的情況下,就是為了更加方便使用一個(gè)特定pipe所以我們需要能夠更加方便: pipe.send() or pipe.asend()說明3:Messages in pynng are immutable; this is to prevent data corruption.警告:可以使用_buffer屬性訪問消息的底層數(shù)據(jù)緩沖區(qū)。但是,必須注意不要在對緩沖區(qū)的引用仍然存在時(shí)發(fā)送消息;如果在消息發(fā)送后使用緩沖區(qū),可能會(huì)導(dǎo)致分段錯(cuò)誤或數(shù)據(jù)損壞(讀: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() 關(guān)閉當(dāng)前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. '''總結(jié)
以上是生活随笔為你收集整理的nanomsg-pynng库的简单学习笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 词条创建
- 下一篇: 易语言之后,中文编程该何去何从?新式中文