[rabbitmq-discuss] publisher confirmations

bergundy roey.berman at gmail.com
Sun Nov 27 15:20:11 GMT 2011


Hi everyone,

I couldn't find code for implementing publisher confirmations.

I wrote this piece of code using pika 0.9.5 (python client).
Would like to get feedback on this, I hope I got it right ...

---
class ConfirmingProducer(object):
    def __init__(self, channel):
        self.delivery_tag = 1
        self.acked        = 0
        self._callbacks   = {}
        self.chan = channel
        self.chan.confirm_delivery(self.on_delivered)

    def publish(self, message, callback, **kwargs):
        self._callbacks[self.delivery_tag] = callback
        self.delivery_tag += 1
        self.chan.basic_publish(message, **kwargs)

    def on_delivered(self, frame):
        success = isinstance(frame.method, pika.spec.Basic.Ack)

        if frame.method.multiple:
            for dtag in range(self.acked+1, frame.method.delivery_tag
+1):
                self._callbacks.pop(dtag)(success)
            self.acked = frame.method.delivery_tag
        else:
            self._callbacks.pop(frame.method.delivery_tag)(success)
            self.acked += 1


More information about the rabbitmq-discuss mailing list