gmr / rabbitpy

A pure python, thread-safe, minimalistic and pythonic RabbitMQ client library
http://rabbitpy.readthedocs.org
BSD 3-Clause "New" or "Revised" License
242 stars 58 forks source link

Chapter 2 bug - drops one message #126

Closed NetwarSystem closed 2 years ago

NetwarSystem commented 3 years ago

If I do this with 10,000 items:

 for line in f:
     print(line)
     time.sleep(.02)
     message = rabbitpy.Message(channel,
                                ujson.dumps(line.rstrip()),
                                {'content_type': 'text/plain'},
                                opinionated=True)
     message.publish(exchange, sys.argv[1])

And then do this to retrieve the data:

while len(queue) > 0:
    message = queue.get()
    print('Message:')
    print(' ID: %s' % message.properties['message_id'])
    print(' Time: %s' % message.properties['timestamp'].isoformat())
    print(' Body: %s' % message.body)
    o.write(str(message.body))
    o.write("\n")
    message.ack()

I consistently get back 9,999 items. I understand that this is not the right way and that it is desirable to "consume" rather than "get", but the RabbitMQ In Depth Book is a treasure, and this library should function correctly. I'm digging in with PyCharm, will post my findings, and a patch if I figure out how to fix it.

gmr commented 3 years ago

I'm afraid there's probably more to this issue than what's in your code, how are you declaring the connection and channel? My guess is your app is exiting before the TCP buffer is fully flushed.

If you're not using a context manager for the connection, you might want to give that a try.