DAS_2023_1/arutunyan_dmitry_lab_4/sushi-bar/consumer1.py

35 lines
1.2 KiB
Python

import pika, time, threading, random
def message_manager(channel, queue_name, exchange_name):
channel.queue_declare(queue=queue_name)
channel.queue_bind(exchange=exchange_name, queue=queue_name)
def callback(ch, method, properties, body):
task = body.decode()
print(f" [x] Received : {task}")
time.sleep(random.randint(2, 3))
if task == "get address":
print(" [x] Address set")
elif task == "get order":
print(" [x] Order sent to preparation")
elif task == "get pavement":
print(" [x] Bank account checked")
else:
print(" [x] Order sent to a delivery")
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_consume(queue=queue_name, on_message_callback=callback)
print("[*] Waiting for messages. To exit press CTRL+C")
channel.start_consuming()
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
exchange_name = 'logs'
queue_name = 'slow-queue'
consumer_thread = threading.Thread(target=message_manager, args=(channel, queue_name, exchange_name))
consumer_thread.start()
consumer_thread.join()