简体   繁体   中英

How to disable heartbeats with pika and rabbitmq

I am using rabbitmq to facilitate some tasks from my rabbit server to my respective consumers. I have noticed that when I run some rather lengthy tests, 20+ minutes, my consumer will lose contact with the producer after it completes it's task. In my rabbit logs, I have seen the error

closing AMQP connection <0.14009.27> (192.168.101.2:64855 -> 
192.168.101.3:5672):
missed heartbeats from client, timeout: 60s

Also, I receive this error from pika

pika.exceptions.ConnectionClosed: (-1, "error(10054, 'An existing connection was forcibly closed by the remote host')")

I'm assuming this is due to this code right here and the conflict of heartbeats with the lengthy blocking connection time.

 self.connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.101.2', 5672, 'user', credentials))
    self.channel = self.connection.channel()
    self.channel.queue_declare(queue=self.tool,
                               arguments={'x-message-ttl': 1000,
                                             "x-dead-letter-exchange": "dlx",
                                             "x-dead-letter-routing-key": "dl",
                                             'durable': True})

Is there a proper way to increase the heartbeat time or how would I turn it off(would it be wise to) completely? Like I said, tests that are 20+ min seem to lead to a closedconnection error but I've ran plenty of tests from the 1-15 minute mark where everything is fine and the consumer client continues to wait for a message to be delivered.

Please don't disable heartbeats. Instead, use Pika correctly. This means:

  • Use Pika version 0.12.0
  • Do your long-running task in a separate thread
  • When your task completes, use the add_callback_threadsafe method to schedule the basic_ack call.

Example code can be found here: link

I'm a RabbitMQ core team member and Pika maintainer so if you have further questions or issues, I recommend following up on either the pika-python or rabbitmq-users mailing list. Thanks!


the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow. RabbitMQ 团队会监控rabbitmq-users邮件列表,并且只是偶尔在 StackOverflow 上回答问题。

You can set the minimum heartbeat interval when creating the connection .

You can see an example in the pika documentation .

I'd recommend against disabling the heartbeat as it might lead to hanging connections piling up on the broker. We experienced such issue in production.

Always make sure the connections have a minimum reasonable heartbeat. If the heartbeat interval needs to be long (hours for example), make sure you close the connection when the application crashes or exits. In this way you won't leave the connection open on the broker side.

As @Luke mentioned, heartbeats are useful but if you still want to disable them, just set heartbeat parameter to zero when creating a connection. So,

  • For URL parameters: connection = pika.BlockingConnection(pika.URLParameters("amqp://user:pass@127.0.0.1?heartbeat=0"))
  • For Connection parameters: connection = pika.BlockingConnection(pika.ConnectionParameters(heartbeat=0))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM