我是 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
原文由 Merlin 发布,翻译遵循 CC BY-SA 4.0 许可协议
据我所知,0MQ 不支持 UDP。此外,IPC 仅在具有符合 POSIX 命名管道实现的操作系统上受支持;因此,在 Windows 上,您实际上只能使用“inproc”、TCP 或 PGM。然而,除此之外,0MQ 的主要特性之一是您的协议只是地址的一部分。您可以举任何例子,更改套接字地址,一切仍然可以正常工作(当然,要遵守上述限制)。此外, ZGuide 有很多示例(其中有很多在 Python 中可用)。