Python ZeroMQ 推/拉——丢失消息? [英] Python ZeroMQ PUSH/PULL -- Lost Messages?
问题描述
我正在尝试在 PUSH/PULL
模式下使用 python
和 zeroMQ
,发送大小为 4[MB]<的消息/strong> 每隔几秒.
I am trying to use python
with zeroMQ
in PUSH / PULL
mode, sending messages of size 4[MB] every few seconds.
出于某种原因,虽然看起来所有消息都已发送,但服务器似乎只收到了其中的一些.我在这里缺少什么?
For some reason, while it looks like all the messages are sent, ONLY SOME of them appear to have been received by the server. What am I missing here?
这是客户端的代码——client.py
import zmq
import struct
# define a string of size 4[MB]
msgToSend = struct.pack('i', 45) * 1000 * 1000
context = zmq.Context()
socket = context.socket(zmq.PUSH)
socket.connect("tcp://127.0.0.1:5000")
# print the message size in bytes
print len(msgToSend)
socket.send(msgToSend)
print "Sent message"
这里是服务器的代码——server.py
And here is the code for the server -- server.py
import zmq
import struct
context = zmq.Context()
socket = context.socket(zmq.PULL)
socket.bind("tcp://127.0.0.1:5000")
while True:
# receive the message
msg = socket.recv()
print "Message Size is: {0} [MB]".format( len(msg) / (1000 * 1000) )
我错过了什么?如何保证消息始终发送且不丢失?
What am I missing? How do I guarantee that messages are always sent and not lost?
以防万一,我使用的是 Ubuntu 10.04
32 位 Core Duo 机器,内存为 2[GB].
In case it matters, I am using Ubuntu 10.04
32bit, Core Duo machine with 2[GB] RAM.
注意:我使用 RabbitMQ
尝试了相同的示例,一切正常——没有消息丢失.我很困惑,因为我经常听到对 zeroMQ
的赞美.为什么在 RabbitMQ
成功的地方失败了?
NOTE: I tried the same example using RabbitMQ
and everything works well -- no message is lost. I am perplexed as I often hear praises of zeroMQ
. Why did it fail where RabbitMQ
succeed?
推荐答案
问题在于,当程序退出时,套接字会立即关闭并以 0 的有效 LINGER 进行垃圾收集(即它会将所有未发送的消息扔掉).对于较大的消息,这是一个问题,因为它们的发送时间比套接字被垃圾收集所需的时间长.
The problem is that when the program exits, the socket gets closed immediately and garbage collected with an effective LINGER of 0 (i.e. it throws any unsent messages away). This is a problem for larger messages because they take longer to send than it takes for the socket to be garbage collected.
您可以通过在程序退出之前放置一个 sleep(0.1)
来避免这种情况(延迟套接字和上下文被垃圾收集).
You can avoid this by putting a sleep(0.1)
just before the program exits (to delay the socket and context being garbage collected).
socket.setsockopt(zmq.LINGER, -1)
(这是默认值)应该可以避免这个问题,但由于某种原因我没有时间调查它并没有.
socket.setsockopt(zmq.LINGER, -1)
(which is the default) should avoid this problem, but it doesn't for some reason that I haven't had time to investigate.
这篇关于Python ZeroMQ 推/拉——丢失消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!