Created
April 16, 2019 01:28
-
-
Save nafeesb/0e64d45cbb283771fe066bc48cc226dc to your computer and use it in GitHub Desktop.
Example PUB/SUB from PyZMQ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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