Last active
August 24, 2022 21:52
-
-
Save ramn/7061042 to your computer and use it in GitHub Desktop.
Python ZeroMQ pub/sub example
This file contains hidden or 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 time.sleep | |
import zmq | |
context = zmq.Context() | |
socket = context.socket(zmq.PUB) | |
socket.bind('tcp://127.0.0.1:2000') | |
# Allow clients to connect before sending data | |
sleep(10) | |
socket.send_pyobj({1:[1,2,3]}) |
This file contains hidden or 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) | |
# We can connect to several endpoints if we desire, and receive from all. | |
socket.connect('tcp://127.0.0.1:2000') | |
# We must declare the socket as of type SUBSCRIBER, and pass a prefix filter. | |
# Here, the filter is the empty string, wich means we receive all messages. | |
# We may subscribe to several filters, thus receiving from all. | |
socket.setsockopt(zmq.SUBSCRIBE, '') | |
message = socket.recv_pyobj() | |
print message.get(1)[2] |
My ide does not detect them either for some reason, but they sure are there:
In [1]: import zmq
In [2]: zmq.PUB
Out[2]: 1
In [3]: zmq.SUB
Out[3]: 2
In [4]: zmq.__version__
Out[4]: '17.1.2'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
zmq.SUB is not references with zmq vrs 17.1.2 ( how to fix it ?)