我正在尝试通过 IPC 实现 PUB/SUB。如果我更改了下面的代码,以便订阅者绑定(bind)到“tcp://*:5000”并且发布者连接到“tcp://localhost:5000”,它可以工作,但我无法让它通过 IPC 工作。我做错了什么?
订阅者.py
import zmq, json
def main():
context = zmq.Context()
subscriber = context.socket(zmq.SUB)
subscriber.bind("ipc://test")
subscriber.setsockopt(zmq.SUBSCRIBE, '')
while True:
print subscriber.recv()
if __name__ == "__main__":
main()
publisher.py
import zmq, json, time
def main():
context = zmq.Context()
publisher = context.socket(zmq.PUB)
publisher.connect("ipc://test")
while True:
publisher.send( "hello world" )
time.sleep( 1 )
if __name__ == "__main__":
main()
最佳答案
最可能的原因是您在不同的目录中运行发布者。尝试使用管道位置的绝对路径:“ipc:///tmp/test.pipe”。您现在使用它的方式使其相对于当前工作目录。
关于python - 无法让 ZeroMQ python 绑定(bind)通过 IPC 接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5060508/