Skip to content

Instantly share code, notes, and snippets.

@vgoklani
Forked from ramn/zeromq_demo_publisher.py
Created May 26, 2017 10:56
Show Gist options
  • Save vgoklani/5cdfd6f846c6cb0bd476cd6ac91a3d94 to your computer and use it in GitHub Desktop.
Save vgoklani/5cdfd6f846c6cb0bd476cd6ac91a3d94 to your computer and use it in GitHub Desktop.
Python ZeroMQ pub/sub example
import time.sleep
import zmq
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind('tcp://127.0.0.1:2000')
# Allow clients to connect before sending data
sleep(10)
socket.send_pyobj({1:[1,2,3]})
import zmq
context = zmq.Context()
socket = context.socket(zmq.SUB)
# We can connect to several endpoints if we desire, and receive from all.
socket.connect('tcp://127.0.0.1:2000')
# We must declare the socket as of type SUBSCRIBER, and pass a prefix filter.
# Here, the filter is the empty string, wich means we receive all messages.
# We may subscribe to several filters, thus receiving from all.
socket.setsockopt(zmq.SUBSCRIBE, '')
message = socket.recv_pyobj()
print message.get(1)[2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment