且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python ZeroMQ 推/拉——丢失消息?

更新时间:2023-11-22 23:15:16

问题在于,当程序退出时,套接字会立即关闭并以 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.