DAS_2024_1/pupkov_alexey_lab_4/Consumer_1.py

26 lines
930 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pika
import time
# Настройка соединения
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
# Создание очереди с фиксированным именем
queue_name = 'orders_slow'
channel.queue_declare(queue=queue_name)
# Binding очереди с exchange
channel.queue_bind(exchange='orders_exchange', queue=queue_name)
# Callback для обработки сообщений
def callback(ch, method, properties, body):
print(f"[Consumer 1] Получено: {body.decode()}")
time.sleep(3) # Эмуляция долгой обработки
print(f"[Consumer 1] Обработано: {body.decode()}")
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_consume(queue=queue_name, on_message_callback=callback)
print("[Consumer 1] Ожидание сообщений...")
channel.start_consuming()