Created
September 28, 2016 14:51
-
-
Save bzamecnik/8bb7266b7048b39455cb604433489557 to your computer and use it in GitHub Desktop.
Simple ZeroMQ publisher/subscriber example.
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 time | |
context = zmq.Context() | |
socket = context.socket(zmq.PUB) | |
socket.connect("tcp://127.0.0.1:5556") | |
# Sleep a bit of time until the connection is properly established, | |
# otherwise some messages may be lost. | |
# http://stackoverflow.com/questions/7470472/lost-messages-on-zeromq-pub-sub | |
time.sleep(1) | |
for i in range(100): | |
message = "foo {}".format(i) | |
print(message) | |
socket.send_string(message) | |
time.sleep(1) |
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 | |
context = zmq.Context() | |
socket = context.socket(zmq.SUB) | |
# accept all topics (prefixed) - default is none | |
socket.setsockopt_string(zmq.SUBSCRIBE, "") | |
socket.bind("tcp://*:5556") | |
while True: | |
message = socket.recv_string() | |
print(message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment