无法获取ZeroMQ python绑定以通过IPC接收消息

2024-05-15 13:11:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试通过IPC实现酒吧/酒吧。如果我更改了下面的代码,以便订阅者绑定到“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()

Tags: pyimportjsonmaindefcontextsocketzmq
1条回答
网友
1楼 · 发布于 2024-05-15 13:11:04

最可能的原因是您正在另一个目录中运行发布服务器。尝试使用管道位置的绝对路径:“ipc:///tmp/test.pipe”。现在使用它的方式使它相对于当前工作目录。

相关问题 更多 >