[rabbitmq-discuss] RabbitMQ -- Patterns For Background Communication?

Marek Majkowski majek04 at gmail.com
Tue Jul 19 11:47:00 BST 2011


Hi!

On Mon, Jul 18, 2011 at 03:22, Shalom Rav <csharpplusproject at gmail.com> wrote:
> I'd like to write a python app (using pika & rabbitMQ) to do the
> following:
>
> There will be ONE *parent*, and TEN *workers*:
>
> Every 2[sec], the *parent* sends a message that must be consumed by
> *all workers*.

Sounds like a every child needs one queue.

> Every 5[sec], each one of the *workers* sends a message directly to
> the *parent*.

And a parent needs one queue as weel.

> __________________________________________________________
>
> Thanks to Jason W., I was able to write two small python scripts
> ('parent.py', 'child.py') that serve as a proof of concept.
>
> Yet, I have one main problem: the two scripts ('parent.py',
> 'child.py') keep exchanging messages between them non-stop. Since BOTH
> the *parent* & the *child* scripts need to publish and consume, I am
> basically 'stuck' with infinite publish / consume loops, leaving no
> room for my actual app to run.
>
> The problem is that I use the line *channel.start_consuming()* in both
> scripts -- which causes the publishing/consuming to run continuously,
> leaving no room for my actual app to do its work.
>
> Given the above case, can you suggest a *background* (or silent, if
> you will) communication pattern that will allow my *actual app* to
> run, and be able to 'send'  / 'receive' messages only at desired
> points in the code -- and not continuously? It looks like the main
> problem is with *channel.start_consuming()*.

Well, you can always run channel.stop_consuming, right?

If you run start_consuming only in order to publish, maybe
you're looking for synchronous publish command. I'm not sure
pika supports it though. There was also a method called
drain_something some time ago, that could be used
to force buffers to be flushed and the message delivered.
Yet another hack is to run a synchronous command after
publish on a channel, in order to synchronize publish.

> I see that one can publish a message ONCE using
> *channel.basic_publish(exchange = 'my_exchange', routing_key = '',
> body = my_message )*; Is there a similar call to consume ONCE (not
> continuously)?

The rough and ugly way of doing that is to use basic.get.

Chers,
 Marek


More information about the rabbitmq-discuss mailing list