Skip to content

Instantly share code, notes, and snippets.

@takluyver takluyver/pull.py
Created Dec 7, 2018

Embed
What would you like to do?
ZMQ push/pull HWM experiment
import zmq
# Set up the socket
ctx = zmq.Context()
sock = ctx.socket(zmq.PULL)
sock.set_hwm(1)
sock.setsockopt(zmq.RCVBUF, 16 * 1024)
sock.connect("tcp://127.0.0.1:8541")
print("RCVHWM", sock.getsockopt(zmq.RCVHWM))
print("RCVBUF", sock.getsockopt(zmq.RCVBUF))
while True:
a = input("Take n messages (blank to quit): ")
if not a:
break
try:
n = int(a)
except TypeError:
continue
# Take n messages from the socket
for _ in range(n):
msg = sock.recv_multipart()
print("Received", msg[0].decode())
import itertools
import time
import zmq
# Set up the socket
ctx = zmq.Context()
sock = ctx.socket(zmq.PUSH)
sock.set_hwm(1)
sock.setsockopt(zmq.SNDBUF, 16 * 1024)
sock.bind("tcp://127.0.0.1:8541")
print("SNDHWM", sock.getsockopt(zmq.SNDHWM))
print("SNDBUF", sock.getsockopt(zmq.SNDBUF))
# Make some dummy data to fill up socket buffers
d = b"abcd" * (1024 * 1024)
# Send an infinite stream of messages
try:
for n in itertools.count():
sock.send_multipart([str(n).encode(), d])
print("Sent", n)
#time.sleep(1)
except KeyboardInterrupt:
pass
sock.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.