Thanks for reviewing, There are several thread on this topic but non was able to lead me to solution, I dont think it's duplicate, as this was not answered on many of the other threads on the subject
I've decided to write another more up to date question we working example.
issue: setting up HWM
does not seem to give to correct effect, am I missing some socket configuration?
expected: pusher
blocked to send more packets until puller
reach 1500 messages
actual: pusher
seem to get blocked only after 70K messages
running the code below I find the PUSH
socket send MANY MANY messages before halting
...
Message [72747::1678881796.1216621]
Message [72748::1678881796.1216683]
Message [72749::1678881796.1216772]
Message [72750::1678881796.121687]
Message [72751::1678881796.1216931]
Socket is blocked! Waiting for the worker to consume messages...
Worker is slow! Waiting...
Worker is slow! Waiting...
below a working example
push
import time
import zmq
context = zmq.Context()
# Set up a PUSH socket with a high water mark of 10 messages for sending
socket = context.socket(zmq.PUSH)
socket.setsockopt(zmq.SNDHWM, 1500)
socket.bind("tcp://127.0.0.1:5566")
# Send messages to the PUSH socket
for i in range(200000):
try:
print(f"Message [{i}::{time.time()}]")
socket.send_string(f"Message [{i}::{time.time()}]", zmq.DONTWAIT)
except zmq.error.Again:
# Handle the situation where the socket is blocked due to the high water mark being reached
print("Socket is blocked! Waiting for the worker to consume messages...")
while True:
# Check whether the socket is ready for sending
# We use poll with a timeout of 1000ms to avoid busy-waiting
if socket.poll(timeout=5000, flags=zmq.POLLOUT):
# If the socket is ready for sending, break out of the loop and try again
break
else:
# If the socket is not ready for sending, wait for the worker to consume messages
print("Worker is slow! Waiting...")
time.sleep(1)
# Clean up
socket.close()
context.term()
pull
import time
import zmq
context = zmq.Context()
receiver = context.socket(zmq.PULL)
receiver.setsockopt(zmq.RCVHWM, 1)
receiver.connect("tcp://127.0.0.1:5566")
def worker():
i = 0
while True:
try:
message = receiver.recv_string(zmq.NOBLOCK)
print(f"Received message: [{i}:{time.time()}]{message}")
time.sleep(500e-3)
i+=1
except zmq.Again:
print("no messages")
time.sleep(100e-3)
worker()