在机器 1 上运行的脚本
import zmq
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.bind("tcp://127.0.0.1:5000")
print "socket bound"
while True:
print "Waiting for message"
message = socket.recv()
print "message received: " + str(message)
这个脚本到达 socket.recv() 然后永远不会从那个调用返回。
发送数据的进程在 machine2 上运行
import zmq
context = zmq.Context()
socket = context.socket(zmq.PUB)
print "socket created"
socket.connect("tcp://machine2:5000")
print "socket connected"
for i in range(1, 3):
print "About to send " + str(i)
socket.send("Hello " + str(i))
print "Sent " + str(i)
print "About to close socket"
socket.close()
print "Socket closed"
执行到完成,但从未完成......
$ python bar.py
socket created
socket connected
About to send 1
Sent 1
About to send 2
Sent 2
About to close socket
Socket closed
我显然做错了,如何创建一个“队列”来接收来自远程主机上发布的多条消息?