Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep pika BlockingConnection alive without disabling heartbeat

I am developing an RabbitMQ consumer with pika 0.10.0, and python 2.7 version.In my consumer client, I have a process that runs for a time period depending on input message. It can vary from 3 to 40 minutes. I do not want to disable heartbeat. Instead I am looking for some collback mechanism that can keep the connection alive until the delivery_tag is sent back. Is that possible?

Few link I got, all are suggesting to disable the heartbeat as workaround. But I do not want to disable it.

Ref:

Socket Error: 104 consuming messages with task that take a long time #753

BlockingConnection gets closed unexpectedly #734

Also, please let me know if any extra information is required. Thanks in advance.

like image 829
Anirban B Avatar asked Sep 05 '17 11:09

Anirban B


People also ask

What is heartbeat in Pika?

Pika picks the highest heartbeat value between client and server (and this is the norm in AMQP clients). This means that if you set a heartbeat value lower than 60 seconds, Pika will always pick RabbitMQ's value because it's higher.

What is RabbitMQ heartbeat?

RabbitMQ heartbeats help the application layer detect interrupted connections and unresponsive peers in a timely manner. Heartbeats also prevent some network devices from disconnecting TCP connections where there is no activity for a certain period of time.

How do I close Pika connection?

The solution is to close your connection in the close-callback of the channel. Also, use a context manager to automatically close the channel. params = pika. ConnectionParameters(host=self.


1 Answers

The only solution is to send heartbeat frames periodically.

When using a BlockingConnection, you have to call the process_data_events function frequently enough (a time_limit of zero is ok). When using a SelectConnection or other async adapters, you have to ensure none of your processes are blocking, so that frames can be sent.

If your task is long running and you can't interrupt or split the process easily for some reason, you can run the task in another thread/process, and still have pika sending frames from the main thread. Just keep in mind that you should avoid using pika connections across threads (pika is not thread-safe at the moment).

like image 119
user8808265 Avatar answered Nov 03 '22 21:11

user8808265