[rabbitmq-discuss] Wrapping pika callbacks, why ?

Raphaël De GIUSTI raphael.degiusti at guardis.com
Tue Aug 7 08:55:03 BST 2012


Hi Gavin,

Hi everyone,
>
> I'm interested in running pika's IOLoop in a thread and I came across this
> code sample :
> http://cms02.phys.ntu.edu.tw/tracs/env/browser/trunk/messaging/pika/mon.py
>
> I was wondering why and what could be the advantages of wrapping the
> callbacks in methods (I can understand the on_exchange_declared() method
> that returns a different callback according to thread's name) ?
>
> As opposed to doing lambda's for callbacks? I'm not sure of the context of
> what you're comparing callback passing style to.
>


For instance, for this callback: connection.callbacks.add(0,
'_on_connection_open', self._on_connected() , one_shot=True )

The on_connected callback function in wrapped in the _on_connected() method

 def _on_connected(self):
        def on_connected(connection):
            pika.log.info("%s: connected : %r " % ( self.tag , connection) )
            connection.channel(self._on_channel_open() )
            self.connection = connection
        return on_connected

Why not: connection.callbacks.add(0, '_on_connection_open',
self.on_connected , one_shot=True )

 def on_connected(self, connection):
        pika.log.info("%s: connected : %r " % ( self.tag , connection) )
        connection.channel(self._on_channel_open() )
        self.connection = connection

> Another question is why considering multiprocessing over threading in this
> case ?
>
> The IOLoop is a singleton instance and you will run into contention issues
> and non-thread safe behaviors in use of the IOLoop and event processing.
>
> I'm not sure what threading buys you in an asynchronous event based model
> of processing but needless complexity.
>
>
My goal is to have an AMQP transport layer inside my application, running
"in background", that handles incoming messages and put them into a python
queue. I also want to be able to publish messages from other threads.
I've got it to work with threads and python queues in a thread-safe way. I
was wondering what could be the benefits for me to use multiprocessing over
threading.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20120807/d5f8c044/attachment.htm>


More information about the rabbitmq-discuss mailing list