DAS_2024_1/tukaeva_alfiya_lab_4/Consumer_1.py

26 lines
820 B
Python
Raw Permalink Normal View History

2024-10-20 19:04:32 +04:00
import random
import time
import pika
queue_name = 'queue_1'
exchange = 'logs'
def callback(ch, method, properties, body):
print(f" [Consumer_1] - получено сообщение - {body.decode()}")
time.sleep(random.choice([2, 3]))
print(f" [Consumer_1] - сообщение обработано")
print()
ch.basic_ack(delivery_tag=method.delivery_tag)
if __name__ == '__main__':
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
try:
channel.queue_declare(queue=queue_name)
channel.queue_bind(exchange=exchange, queue=queue_name)
channel.basic_consume(queue=queue_name, on_message_callback=callback)
channel.start_consuming()
except KeyboardInterrupt:
connection.close()