Код IT
← Каталог

RabbitMQ — Consumer pattern

Фрагмент из «RabbitMQ»: Consumer pattern.

python infra-securityencyclopedia8-05-mikroservisy-i-integratsiya-118 embed URL статья в энциклопедии
Python main.py
def callback(ch, method, properties, body):
    try:
        process(body)
        ch.basic_ack(delivery_tag=method.delivery_tag)
    except Exception:
        ch.basic_nack(
            delivery_tag=method.delivery_tag,
            requeue=False  # отправка в DLX при настройке
        )

channel.basic_qos(prefetch_count=3)
channel.basic_consume(
    queue='Задачи',
    on_message_callback=callback,
    auto_ack=False
)
channel.start_consuming()
def callback(ch, method, properties, body):
    try:
        process(body)
        ch.basic_ack(delivery_tag=method.delivery_tag)
    except Exception:
        ch.basic_nack(
            delivery_tag=method.delivery_tag,
            requeue=False  # отправка в DLX при настройке
        )

channel.basic_qos(prefetch_count=3)
channel.basic_consume(
    queue='Задачи',
    on_message_callback=callback,
    auto_ack=False
)
channel.start_consuming()