Created
February 15, 2018 14:47
-
-
Save uberscientist/1f0ce9e754a5a448e943285347a4d9c0 to your computer and use it in GitHub Desktop.
pyzmq PUB/SUB 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 | |
from multiprocessing import Process | |
from time import sleep | |
""" | |
Simple multiprocess pub/sub ZeroMQ example python3 | |
2/15/2018 | |
""" | |
tcp_interface = 'tcp://127.0.0.1:5600' | |
def publisher(): | |
i = 0 | |
pub_ctx = zmq.Context() | |
pub_sock = pub_ctx.socket(zmq.PUB) | |
pub_sock.bind(tcp_interface) | |
while True: | |
sleep(1) | |
i += 1 | |
pub_sock.send_string('test {i}'.format(i=i)) | |
def subscriber(): | |
sub_ctx = zmq.Context() | |
sub_sock = sub_ctx.socket(zmq.SUB) | |
sub_sock.setsockopt(zmq.SUBSCRIBE, b"") | |
sub_sock.connect(tcp_interface) | |
while True: | |
print(sub_sock.recv_string()) | |
Process(target=subscriber).start() | |
Process(target=publisher).start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment