[rabbitmq-discuss] Using Pika Python client library with BlockingConnections and publisher-confirms - Does it work?

Ask Solem ask at rabbitmq.com
Mon Feb 6 13:34:05 GMT 2012


On 6 Feb 2012, at 13:13, Marek Majkowski wrote:

> On Mon, Feb 6, 2012 at 12:40, Ask Solem <ask at rabbitmq.com> wrote:
>> On 6 Feb 2012, at 11:59, Marek Majkowski wrote:
>> 
>>> <shameless plug>
>>> It's rather simple using Puka:
>>> 
>>>   client = puka.Client("amqp://localhost/")
>>> 
>>>   promise = client.connect()
>>>   client.wait(promise)
>>> 
>>>   promise = client.basic_publish(exchange='', routing_key='test',
>>> body="Hello world!")
>>>   client.wait(promise)
>>> 
>>> https://github.com/majek/puka/blob/master/examples/send.py
>>> </shameless plug>
>> 
>> 
>> But that doesn't use publisher confirms?
> 
> Internally it does. `client.wait` will block until pub-ack or an
> error comes back.

Right, in pika/kombu you would use drain_events(), but that doesn't change
the points I presented.  Though, kombu/pika donsn't have a simple solution to wait
for a specific event like pika does (afaik).

If I had to do it synchronously, I would not do it on an event by event basis,
but instead limit the active number of publishes, something like:

class Confirming(object):
    active = 0
    limit = 10
    timeout = 180

    def __init__(self, channel, limit=None, timeout=None):
        self.channel = channel
        self.limit = limit if limit is not None else self.limit
        self.timeout = timeout or self.timeout

    def confirm_callback(self):
        self.active -= 1

    def publish(self, body, **options):
        self.active += 1
        while self.active >= self.limit:
            self.connection.drain_events(timeout=self.max_timeout)
        self.channel.basic_publish(message, **options)
        self.connection.drain_events(timeout=0.0001)


More information about the rabbitmq-discuss mailing list