nanomsg-pynng库的简单学习笔记

柳才良
2023-12-01
'''
'''
'''
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 Sub
    class pynng.Pub0(**kwargs)
    class pynng.Sub0(**kwargs)
    
    pub:不能使用recv 否则会提示 pynng.NotSupported exception
    sub:接收同样的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 Pull
    class 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、Bus0
    class pynng.Bus0(**kwargs)
    Bus0:发送一个msg 给所有直接相联的peers,这个也就允许我们设计一个网格网络,这个msg也就仅仅发送给直接相联的peers
         You 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、Context
    class 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、Message
    class 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 Dialer
    close() 关闭当前dialer
5、Listener
    class 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.listeners
    
    
    close()
        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 Sub
    class pynng.Pub0(**kwargs)
    class pynng.Sub0(**kwargs)
    
    pub:不能使用recv 否则会提示 pynng.NotSupported exception
    sub:接收同样的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 Pull
    class 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、Bus0
    class pynng.Bus0(**kwargs)
    Bus0:发送一个msg 给所有直接相联的peers,这个也就允许我们设计一个网格网络,这个msg也就仅仅发送给直接相联的peers
         You 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、Context
    class 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、Message
    class 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 Dialer
    close() 关闭当前dialer
5、Listener
    class 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.listeners
    
    
    close()
        Close the listener.
'''
 类似资料: