Created
December 7, 2018 10:29
-
-
Save takluyver/a916abfe368010333c345b1fcb3d47c7 to your computer and use it in GitHub Desktop.
ZMQ push/pull HWM experiment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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