Created
March 8, 2020 08:32
-
-
Save Dansyuqri/c7e8f4bf1d93cf376da8374d0324767b to your computer and use it in GitHub Desktop.
Sending/receiving simple messages using 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
# simple_pub.py | |
import zmq | |
import time | |
host = "127.0.0.1" | |
port = "5001" | |
# Creates a socket instance | |
context = zmq.Context() | |
socket = context.socket(zmq.PUB) | |
# Binds the socket to a predefined port on localhost | |
socket.bind("tcp://{}:{}".format(host, port)) | |
time.sleep(1) | |
# Sends a string message | |
socket.send_string("ML hello") |
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
# simple_sub.py | |
import zmq | |
host = "127.0.0.1" | |
port = "5001" | |
# Creates a socket instance | |
context = zmq.Context() | |
socket = context.socket(zmq.SUB) | |
# Connects to a bound socket | |
socket.connect("tcp://{}:{}".format(host, port)) | |
# Subscribes to all topics | |
socket.subscribe("ML") | |
# Receives a string format message | |
socket.recv_string() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment