Below is my rabbitmq snippet. I am running long running task, connection is getting closed though my task continue to run in the background. But, when connection is getting closed, older msg get requeued and picked by another worker. How to avoid connection getting closed? I do see this thread talks about using threading: Handling long running tasks in pika / RabbitMQ
But, then there are answer that inform pika not being thread safe. Any better solution? apart from threading and apart from setting heartbeat to 0?
def consume(self):
self._rabbitmq_channel.basic_qos(prefetch_count=self._prefetch_count)
self._rabbitmq_channel.basic_consume(queue=self._rabbitmq_queue, on_message_callback=self._run_report)
try:
self._rabbitmq_channel.start_consuming()
except (pika.exceptions.AMQPConnectionError, pika.exceptions.AMQPChannelError,
pika.exceptions.ChannelWrongStateError) as e:
self._reconnect()
self.consume()
def _reconnect(self):
try:
self._rabbitmq_connection = self._connect()
self._rabbitmq_channel = self._get_channel()
except pika.exceptions.AMQPConnectionError as e:
raise e
def _connect(self):
return pika.BlockingConnection(pika.ConnectionParameters(host=self._rabbitmq_host,
credentials=self._credentials,
heartbeat=6000,
blocked_connection_timeout=6000
))
def _get_channel(self):
return self._rabbitmq_connection.channel()