问题描述
我正在尝试通过IPC实现PUB/SUB.如果我更改了下面的代码,以使订阅者绑定到"tcp://*:5000",而发布者连接到"tcp://localhost:5000",则它可以工作,但是我无法使其在IPC上工作.我在做什么错了?
I'm trying to achieve PUB/SUB over IPC. If I changed the code below so that the subscriber binds to "tcp://*:5000" and the publisher connects to "tcp://localhost:5000" it works, but I can't get it to work over IPC. What am I doing wrong?
subscriber.py
subscriber.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
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".现在,您使用它的方式使其相对于当前工作目录.
most likely cause is that you are running the publisher in a different directory. Try using absolute path for the pipe location: "ipc:///tmp/test.pipe". The way you are using it now makes it relative to current working directory.
这篇关于无法获取ZeroMQ python绑定以通过IPC接收消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!