Skip to content

Instantly share code, notes, and snippets.

@wwj718
Forked from ramn/zeromq_demo_publisher.py
Last active March 16, 2018 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wwj718/994722678f371eaefbc42d229788248a to your computer and use it in GitHub Desktop.
Save wwj718/994722678f371eaefbc42d229788248a to your computer and use it in GitHub Desktop.
Python ZeroMQ pub/sub example
'''
测试环境:
python3.6
pyzmq==17.0.0
'''
import time
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
for i in range(1,100):
socket.send_pyobj({i:[1,2,3]})
time.sleep(1)
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, '')
while True:
socket.setsockopt_string(zmq.SUBSCRIBE, '')
message = socket.recv_pyobj()
print(str(message))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment