Skip to content Skip to sidebar Skip to footer

How To Have Limited Zmq (zeromq - Pyzmq) Queue Buffer Size In Python?

I'm using pyzmq library with pub/sub pattern. I have some quick ZMQ publisher by .connect() method and a slower ZMQ subscriber by .bind() method. Then after a few minutes, my subsc

Solution 1:

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.while1:
    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)


Solution 2:

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)

Post a Comment for "How To Have Limited Zmq (zeromq - Pyzmq) Queue Buffer Size In Python?"