Skip to content

Instantly share code, notes, and snippets.

@Dansyuqri
Last active February 22, 2023 07:05
Show Gist options
  • Save Dansyuqri/9eea1c4affa27b9d5ad138fd508e8026 to your computer and use it in GitHub Desktop.
Save Dansyuqri/9eea1c4affa27b9d5ad138fd508e8026 to your computer and use it in GitHub Desktop.
Sending/receiving multipart messages using PyZMQ
# multipart_pub.py
import zmq
import time
host = "127.0.0.1"
port = "5001"
# Creates a socket instance
context = zmq.Context()
socket = context.socket(zmq.PUB)
# Binds the socket to a predefined port on localhost
socket.bind("tcp://{}:{}".format(host, port))
time.sleep(1)
# Sends a multipart message
socket.send_multipart([b"ML", b"hello"])
# multipart_sub.py
import zmq
host = "127.0.0.1"
port = "5001"
# Creates a socket instance
context = zmq.Context()
socket = context.socket(zmq.SUB)
# Connects to a bound socket
socket.connect("tcp://{}:{}".format(host, port))
# Subscribes to all topics
socket.subscribe("ML")
# Receives a multipart message
print(socket.recv_multipart())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment