Pika Blocking Connection with Timeout

2,504 views
Skip to first unread message

Perry Taylor

unread,
Apr 4, 2019, 3:47:04 PM4/4/19
to rabbitmq-users
We are migrating from WebsphereMQ to RabbitMQ.  Currently in WebsphereMQ we are using the AMQ API that provides a get blocking connection while waiting for available messages.  We can specify a timeout value after which if there have been no new messages on the queue the client will unblock, returning control to the calling application.   I can't seem to find similar functionality with pika's blocking connection using basic_get.  Am I just missing something or does pika not provide such functionality?

Thanks.

Luke Bakken

unread,
Apr 4, 2019, 5:53:51 PM4/4/19
to rabbitmq-users
Hi Perry,

Don't use basic_get. Instead, use consume in a for loop as it allows you to specify an inactivity timeout:

Luke

Perry Taylor

unread,
Apr 5, 2019, 9:41:12 AM4/5/19
to rabbitmq-users
Thank you very much for the reply Luke.

I actually saw an example of this in the pka documentation, 2.3.5 "Using the BlockingChannel.consume generator to consume Messages". It left me wondering what would happen if we had multiple processes consuming messages from the same queue. Doesn't the consume method ask for all messages on the queue?  Of course we could ack the first message then cancel but what happens to other processes trying to grab messages from the same queue.  Are there any performance or other considerations here?  Would there be conflicts?

Thanks
Perry

Luke Bakken

unread,
Apr 5, 2019, 10:32:32 AM4/5/19
to rabbitmq-users
Hi Perry,

You use the channel's QoS value (also known as prefetch) to tell RabbitMQ how many unacknowledged messages to deliver to the consumer. So, if you call channel.basic_qos(prefetch_count=1) you know that consumer will only get one message at a time. You can have all conusmers set a prefetch_count and RabbitMQ will round-robin message deliveries to them.

This is a much better mechanism to use when compared to basic.get and polling.

FYI, these concepts are all covered in the RabbitMQ tutorials - https://www.rabbitmq.com/tutorials/tutorial-one-python.html

Thanks,
Luke

Perry Taylor

unread,
Apr 5, 2019, 10:36:24 AM4/5/19
to rabbitmq-users
Hello Luke,

That's sounds like exactly what I was looking for!  Thank you very much for the assist!

-Perry
Reply all
Reply to author
Forward
0 new messages