Wait for a single RabbitMQ message with a timeout
Posted
by Evgeny
on Stack Overflow
See other posts from Stack Overflow
or by Evgeny
Published on 2010-05-10T00:26:43Z
Indexed on
2010/05/10
0:38 UTC
Read the original article
Hit count: 812
I'd like to send a message to a RabbitMQ server and then wait for a reply message (on a "reply-to" queue). Of course, I don't want to wait forever in case the application processing these messages is down - there needs to be a timeout. It sounds like a very basic task, yet I can't find a way to do this. I've now run into this problem with both py-amqplib and the RabbitMQ .NET client.
The best solution I've got so far is to poll using basic_get
with sleep
in-between, but this is pretty ugly:
def _wait_for_message_with_timeout(channel, queue_name, timeout):
slept = 0
sleep_interval = 0.1
while slept < timeout:
reply = channel.basic_get(queue_name)
if reply is not None:
return reply
time.sleep(sleep_interval)
slept += sleep_interval
raise Exception('Timeout (%g seconds) expired while waiting for an MQ response.' % timeout)
Surely there is some better way?
© Stack Overflow or respective owner