为什么 Zeromq 不能在本地主机上工作?
这段代码工作得很好:
import zmq, json, time
def main():
context = zmq.Context()
subscriber = context.socket(zmq.SUB)
subscriber.bind("ipc://test")
subscriber.setsockopt(zmq.SUBSCRIBE, '')
while True:
print subscriber.recv()
def main():
context = zmq.Context()
publisher = context.socket(zmq.PUB)
publisher.connect("ipc://test")
while True:
publisher.send( "hello world" )
time.sleep( 1 )
但是这段代码不*工作:
import zmq, json, time
def recv():
context = zmq.Context()
subscriber = context.socket(zmq.SUB)
subscriber.bind("tcp://localhost:5555")
subscriber.setsockopt(zmq.SUBSCRIBE, '')
while True:
print subscriber.recv()
def send():
context = zmq.Context()
publisher = context.socket(zmq.PUB)
publisher.connect("tcp://localhost:5555")
while True:
publisher.send( "hello world" )
time.sleep( 1 )
它引发了这个错误:
ZMQError:没有这样的设备
为什么,zeromq 不能使用 localhost 接口?
只能在同一台机器上的IPC上使用吗?
This code works great:
import zmq, json, time
def main():
context = zmq.Context()
subscriber = context.socket(zmq.SUB)
subscriber.bind("ipc://test")
subscriber.setsockopt(zmq.SUBSCRIBE, '')
while True:
print subscriber.recv()
def main():
context = zmq.Context()
publisher = context.socket(zmq.PUB)
publisher.connect("ipc://test")
while True:
publisher.send( "hello world" )
time.sleep( 1 )
But this code doesn't* work:
import zmq, json, time
def recv():
context = zmq.Context()
subscriber = context.socket(zmq.SUB)
subscriber.bind("tcp://localhost:5555")
subscriber.setsockopt(zmq.SUBSCRIBE, '')
while True:
print subscriber.recv()
def send():
context = zmq.Context()
publisher = context.socket(zmq.PUB)
publisher.connect("tcp://localhost:5555")
while True:
publisher.send( "hello world" )
time.sleep( 1 )
It raises this error:
ZMQError: No such device
Why, can't zeromq use localhost interfaces?
Does it only work on IPC on the same machine?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 @fdb 指出的:
问题出在 line:
try 更改为:
但是,这需要更多解释以理解原因。
zmq_bind 的文档解释了(粗体强调是我的):
由于您的示例使用 tcp 作为传输协议,我们在 zmq_tcp 文档来发现(再次强调我的):
因此,如果您不使用通配符或接口名称,则意味着您必须使用数字形式的 IPv4 地址(而不是 DNS 名称)。
注意,这只适用于使用
zmq_bind
!另一方面,使用zmq_connect
的 DNS 名称是完全可以的,正如稍后在 zmq_tcp:As @fdb points out:
The problem is at line:
try to change to:
However this deserves more explanation to understand why.
The documentation for zmq_bind explains (bold emphasis mine):
Since your example uses tcp as the transport protocol we look in the zmq_tcp documentation to discover (again, bold emphasis mine):
So, if you're not using wild-card or the interface name, then it means you must use an IPv4 address in numeric form (not a DNS name).
Note, this only applies to the use of
zmq_bind
! On the other hand it is perfectly fine to use a DNS name withzmq_connect
as discussed later in the docs for zmq_tcp:问题出在行:
尝试更改为:
The problem is at line:
try to change to: