All the examples in pika tutorial end with the client invoking start_consuming()
, which starts an infinite loop. These examples work for me.
However, I do not want my client to run forever. Instead, I need my client to consume messages for some time, such as 15 minutes, then stop.
How do I accomplish that?
You can consume messages one at a time with your own loops, say you have a
channel
andqueue
setup. The following will check if the queue is empty, and if not, pop a single message off of it.declaring a queue that already exists, and setting the passive flags allows you to query it's state. Next we process a message:
Here
callback_func
is our normal callback. Make sure not to register the callback with the queue when you want to process this way.This will make the manual consume do weird things. I have seen the queue_declare code actually process a message if I have made this call beforehand.