[rabbitmq-discuss] RabbitMQ Usage Patterms?

Marek Majkowski majek04 at gmail.com
Tue Jul 19 14:20:05 BST 2011


On Tue, Jul 19, 2011 at 03:31, csharpplusproject
<csharpplusproject at gmail.com> wrote:
> Thank you for your explanation, although I do not really understand how this
> works.
>
> You write: "...listening for messages arriving from RabbitMQ and dispatching
> them to your application."
>
> I have no problem writing code that is "...listening for messages arriving
> from RabbitMQ". The question is, how do I dispatch these messages to my
> application while letting the application run continuously with no
> interruptions?
>
> Can anybody provide sample code in python that demonstrates such a pattern?

Okay, where to start....

When Rabbitmq (or any other network application) sends data to tcp/ip
connection,
well, it is encoded as electrons on the wire.

On the other side of the wire, maybe on different computer, a network card
receives electrons, decodes them and appends to a tcp/ip receive
buffer for a particular tcp/ip connection.

That was easy. Now the hard part. It's the application that needs to ask
the kernel: "is there more data available for me in the buffers?"

Asking this question all over again would be quite inefficient, so it's usually
put as a blocking request: "give me this number of bytes of data from
my tcp/ip buffers, wait if there isn't enough yet".

Most AMQP clients follow this pattern: you can do CPU intensive
work in your application *or* you can wait on the network and
receive and send data.

If you want to listen to messages: you shall block your program and wait
for network traffic.

Coming back to your question:
> The question is, how do I dispatch these messages to my
> application while letting the application run continuously with no
> interruptions?

You don't. If you want to receive data, you need to or:
 - block program and hang on socket till data is received
 - or poll kernel asking for more data which is quite inefficient

In normal programming languages people use threads to do that,
one thread does CPU stuff, the other does network stuff.
But a) threads in general are complex b) threads in python are weird.

Hope that helps,
  Marek


More information about the rabbitmq-discuss mailing list