如何使用Zeromq的inproc和ipc传输?

2024-06-11 03:09:10 发布

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

我是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

Tags: toimportsendmessagerequestconnectcontextsocket
3条回答

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

  • 您必须使用收音机/碟子模式(非常类似于Pub/Sub)
  • libzmq和czmq支持
  • 参见libzmq源代码中的tests/test_udp.cpptests/test_radio_dish.cpp
  • Doron Somech在zeromq dev@list线程上提供的完整分解:Thread safe Pub/Sub and Multicast

如果(and only if)使用ZMQ_PUB或ZMQ_SUB sockets(在您给出的示例中没有这样做,在这里您使用路由器、XREQ等),则可以使用UDP,或者更准确地说,通过

"epgm://host:port"

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

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

不过,我不知道UDP是否支持单播场景。

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

相关问题 更多 >