Skip to content

Instantly share code, notes, and snippets.

@nafeesb
Created April 16, 2019 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nafeesb/0e64d45cbb283771fe066bc48cc226dc to your computer and use it in GitHub Desktop.
Save nafeesb/0e64d45cbb283771fe066bc48cc226dc to your computer and use it in GitHub Desktop.
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