From 2ed1e4f4ae2619dc6578161b9d48c7110816d875 Mon Sep 17 00:00:00 2001 From: Mostafa Date: Sat, 18 Jan 2025 23:23:46 +0800 Subject: [PATCH 1/2] chore: add example for ZMQ --- examples/example_zmq.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 examples/example_zmq.py diff --git a/examples/example_zmq.py b/examples/example_zmq.py new file mode 100644 index 0000000..3c637db --- /dev/null +++ b/examples/example_zmq.py @@ -0,0 +1,38 @@ +import sys +import zmq + + +def main() -> None: + if len(sys.argv) < 2: + print('Usage: python3 ./example_zmq.py [topic topic ...]') + sys.exit(1) + + connect_to = sys.argv[1] + topics = sys.argv[2:] + ctx = zmq.Context() + s = ctx.socket(zmq.SUB) + s.connect(connect_to) + + # manage subscriptions + if not topics: + print("Receiving messages on ALL topics...") + s.setsockopt(zmq.SUBSCRIBE, b'') + else: + print(f"Receiving messages on topics: {topics} ...") + for t in topics: + s.setsockopt(zmq.SUBSCRIBE, bytes.fromhex(t)) + print + try: + while True: + msg = s.recv_multipart() + hex_string = [b.hex() for b in msg] + print('msg: {}'.format(hex_string)) + + + except KeyboardInterrupt: + pass + print("Done.") + + +if __name__ == "__main__": + main() \ No newline at end of file From ef3fb6966256319812a6302e41e4800f2e8aa2b6 Mon Sep 17 00:00:00 2001 From: Mostafa Date: Sun, 19 Jan 2025 01:00:39 +0800 Subject: [PATCH 2/2] chore: fix testing issues --- examples/example_zmq.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/examples/example_zmq.py b/examples/example_zmq.py index 3c637db..7ade94c 100644 --- a/examples/example_zmq.py +++ b/examples/example_zmq.py @@ -4,7 +4,7 @@ def main() -> None: if len(sys.argv) < 2: - print('Usage: python3 ./example_zmq.py [topic topic ...]') + print("Usage: python3 ./example_zmq.py [topic topic ...]") sys.exit(1) connect_to = sys.argv[1] @@ -16,7 +16,7 @@ def main() -> None: # manage subscriptions if not topics: print("Receiving messages on ALL topics...") - s.setsockopt(zmq.SUBSCRIBE, b'') + s.setsockopt(zmq.SUBSCRIBE, b"") else: print(f"Receiving messages on topics: {topics} ...") for t in topics: @@ -26,8 +26,7 @@ def main() -> None: while True: msg = s.recv_multipart() hex_string = [b.hex() for b in msg] - print('msg: {}'.format(hex_string)) - + print("msg: {}".format(hex_string)) except KeyboardInterrupt: pass @@ -35,4 +34,4 @@ def main() -> None: if __name__ == "__main__": - main() \ No newline at end of file + main()