Python ZeroMQ PUSH/PULL — Lost Messages?

后端 未结 2 1018
小鲜肉
小鲜肉 2021-02-09 01:20

I am trying to use python with zeroMQ in PUSH / PULL mode, sending messages of size 4[MB] every few seconds.

For

相关标签:
2条回答
  • 2021-02-09 01:53

    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.

    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) (which is the default) should avoid this problem, but it doesn't for some reason that I haven't had time to investigate.

    0 讨论(0)
  • 2021-02-09 02:04

    It's conceivable that you are running out of memory (depending how you are sending messages, whether they are being consumed fast enough, etc). You can use socket.setsockopt(zmq.HWM) to set HWM to a sane value and prevent zeromq from storing too many messages in the outgoing buffer. With this in mind, consider slightly modified examples:

    # server
    ...
    counter = 0
    while True:
        ...receive the message
        counter += 1
        print "Total messages recieved: {0}".format(counter)
    
    # client
    socket.setsockopt(zmq.HWM, 8)
    for i in range(1000):
        socket.send(msgToSend)
    

    And then running 10 test clients:

    for i in {1..10}; do
        python client.py &
    done
    

    From the server you can see all the messages are recieved:

    Total messages recieved: 9998
    Total messages recieved: 9999
    Total messages recieved: 10000
    
    0 讨论(0)
提交回复
热议问题