Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example PUB/SUB from PyZMQ
import zmq
import sys,os,time
#%%
port = '5563'
def server():
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind('tcp://*:%s' % port)
while True:
socket.send_multipart([b"A", b"We don't want to see this"])
socket.send_multipart([b"B", b"We would like to see this"])
time.sleep(1)
socket.close()
context.term()
def client():
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect('tcp://localhost:%s' % port)
socket.setsockopt(zmq.SUBSCRIBE, b"B")
while True:
[addr, data] = socket.recv_multipart()
print('[%s]: %s' % ( addr, data ) )
socket.close()
context.term()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment