py-amqp icon indicating copy to clipboard operation
py-amqp copied to clipboard

pyamqp won't send heartbeat?

Open Hoohaha opened this issue 3 years ago • 0 comments

pyamqp==5.1.1

I used below sample code for testing. If set heartbeat, the connection will be killed by rabbitmq server.

If disable heartbeat by set zero value, the code works very well.

Seems pyamqp won't send heartbeat automatically.

But if no heartbeat the rabbitmq won't close died connections, it cause server performance if too many dided connections.

import amqp
import time
import logging

def _on_message(*args, **kwargs):
	pass

def main():
	connection = amqp.Connection(host = 'xxx.xxx.xxx.xxx:5672', heartbeat = 3, userid="xxx", password="xxx")
	connection.connect()
	channel = connection.channel()

	queue = channel.queue_declare(queue = 'myqueue', durable = False, auto_delete = False)
	queue_name = queue.queue
	channel.exchange_declare('myex', type='direct', durable = False, auto_delete = False)

	channel.queue_bind(queue_name, 'myex', 'myroutingkey')
	channel.basic_qos(prefetch_size = 0, prefetch_count = 2, a_global = False)
	channel.basic_consume(callback = _on_message, queue = queue_name)
	print("xxxxxxxxxxxxxxxxxxxxxx")

	try:
		while True:
			connection.drain_events()
			time.sleep(0.5)
	except KeyboardInterrupt:
		logging.info("Python service stoped...........^v^")
		# Someone pressed CTRL-C, stop consuming and close
	except Exception as ex:
		logging.critical(ex)
	finally:
		channel.close()
		connection.close()

main()

Hoohaha avatar Jun 23 '22 03:06 Hoohaha