18

我是 ZERMQ 的新手。ZeroMQ 有 TCP、INPROC 和 IPC 传输。我正在寻找在 Winx64 和 python 2.7 中使用 python 和 inproc 的示例,它们也可用于 linux。

另外,我一直在寻找 UDP 传输方法,但找不到示例。

我发现的唯一例子是

import zmq
import zhelpers

context = zmq.Context()

sink = context.socket(zmq.ROUTER)
sink.bind("inproc://example")

# First allow 0MQ to set the identity
anonymous = context.socket(zmq.XREQ)
anonymous.connect("inproc://example")
anonymous.send("XREP uses a generated UUID")
zhelpers.dump(sink)

# Then set the identity ourself
identified = context.socket(zmq.XREQ)
identified.setsockopt(zmq.IDENTITY, "Hello")
identified.connect("inproc://example")
identified.send("XREP socket uses REQ's socket identity")
zhelpers.dump(sink)

我正在考虑的用例是:UDP 之类的信息分发。使用 TCP 测试 Push/Pull 更快,或者 inproc 更快。

这是测试示例>.......

服务器:

import zmq
import time

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("inproc://example2")

while True:
    #  Wait for next request from client
    message = socket.recv()
    print "Received request: ", message

    #  Do some 'work'
    time.sleep (1)        #   Do some 'work'

    #  Send reply back to client
    socket.send("World")

客户:

import zmq

context = zmq.Context()

#  Socket to talk to server
print "Connecting to hello world server..."
socket = context.socket(zmq.REQ)
socket.connect ("inproc://example2")

#  Do 10 requests, waiting each time for a response
for request in range (1,10):
    print "Sending request ", request,"..."
    socket.send ("Hello")

    #  Get the reply.
    message = socket.recv()
    print "Received reply ", request, "[", message, "]"

错误消息:

 socket.connect ("inproc://example2")
File "socket.pyx", line 547, in zmq.core.socket.Socket.connect (zmq\core\socket.c:5347)
zmq.core.error.ZMQError: Connection refused
4

4 回答 4

14

据我所知,0MQ 不支持 UDP。此外,IPC 仅在具有符合 POSIX 的命名管道实现的操作系统上受支持;因此,在 Windows 上,您实际上只能使用“inproc”、TCP 或 PGM。然而,除此之外,0MQ 的主要特性之一是您的协议只是地址的一部分。您可以举任何例子,更改套接字地址,一切应该仍然可以正常工作(当然,受上述限制)。此外,ZGuide有很多示例(其中很多在Python中可用)。

于 2011-12-13T16:27:32.987 回答
9

如果(且仅当)您使用 ZMQ_PUB 或 ZMQ_SUB 套接字 - 在您给出的示例中没有这样做,您使用 ROUTER、XREQ 等 - 您可以使用 UDP,或者更准确地说,通过UDP 多播

“epgm://主机:端口”

EPGM代表Encapsulated PGM,即封装在UDP中的PGM,比原始PGM更兼容现有的网络基础设施。

另见http://api.zeromq.org/2-1:zmq-pgm

不过,我不知道对单播场景有任何 UDP 支持。

于 2012-07-05T06:16:44.140 回答
5

自 2016 年 3 月起,ZeroMQ 支持线程安全的 UDP:

  • 您必须使用 Radio/Dish 模式(非常类似于 Pub/Sub)
  • 在 libzmq 和 czmq 中受支持
  • 请参阅tests/test_udp.cpptests/test_radio_dish.cpp在 libzmq 源代码中
  • Doron Somech 在 zeromq-dev@ 列表线程上提供的完整细分:线程安全的 Pub/Sub 和多播
于 2016-04-11T19:44:22.670 回答
0

当我的pyzmq和zmq的版本是旧版本时,我遇到了同样的问题,我将版本升级到15.2.0,然后解决了问题,我使用的ipc地址前缀是“inproc://”

操作系统:win7-x64 蟒蛇:2.7.6

于 2016-02-18T02:12:03.567 回答