Snippets

Frederik Banke Python SimpleConsumer

Created by Frederik Banke
import pika


class SimpleConsumer(object):
    def __init__(self, channel, consume_queue_name):
        self.channel = channel

        self.properties = pika.BasicProperties(
            delivery_mode=2,  # make message persistent
        )

        self.channel.queue_declare(queue=consume_queue_name, durable=True)

        self.channel.basic_consume(self.callback,
                                   queue=consume_queue_name,
                                   no_ack=False)

        self.channel.start_consuming()

    def callback(self, ch: pika.adapters.blocking_connection.BlockingChannel, method, _, body):
        print(body)

        ch.basic_ack(delivery_tag=method.delivery_tag)


if __name__ == '__main__':
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

    consumer = SimpleConsumer(connection.channel(), 'queue_name')

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.