How to have limited ZMQ (ZeroMQ - PyZMQ) queue buffer size in python?

前端 未结 2 731
半阙折子戏
半阙折子戏 2020-12-18 09:04

I\'m using pyzmq library with pub/sub pattern. I have some quick ZMQ publisher by .connect() method and a

相关标签:
2条回答
  • 2020-12-18 09:45

    To set the queue/buffer size you need to set the high water marks via the socket options

    setsockopt(zmq.SNDHWM, 10)
    setsockopt(zmq.RCVHWM, 10)
    
    0 讨论(0)
  • 2020-12-18 09:57

    I found a way to get "Last message only" option in ZMQ Subscribe socket (using CONFLATE option).

    But first you should set the CONFLATE option before you connect:

    import zmq
    import time
    
    port = "5556"
    context = zmq.Context()
    socket = context.socket(zmq.SUB)
    
    socket.setsockopt(zmq.SUBSCRIBE, '')
    socket.setsockopt(zmq.CONFLATE, 1)  # last msg only.
    socket.connect("tcp://localhost:%s" % port)  # must be placed after above options.
    
    while 1:
        time.sleep(2)  # Dummy delay
        data = socket.recv()
        print(data)
    

    On the other word, I removed any buffered queue in subscriber code.


    [NOTE]:

    In addition, with the zmq.SNDBUF and zmq.RCVBUF options we could set a limit on ZMQ buffer size. (More complete and an example)


    0 讨论(0)
提交回复
热议问题