DAS_2023_1/antonov_dmitry_lab4/rabbitmq/consumer1.py

27 lines
841 B
Python
Raw Normal View History

2023-12-04 22:18:07 +04:00
import pika
2023-12-05 12:35:35 +04:00
2023-12-04 22:18:07 +04:00
def callback(ch, method, properties, body):
2023-12-05 12:35:35 +04:00
print("Врач 1 принимает пациента '{}'".format(body.decode()))
2023-12-04 22:18:07 +04:00
def consume_messages():
2023-12-05 12:35:35 +04:00
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
2023-12-04 22:18:07 +04:00
channel = connection.channel()
2023-12-05 12:35:35 +04:00
exchange_name = 'logs'
2023-12-04 22:18:07 +04:00
channel.queue_declare(queue='example_queue')
2023-12-05 12:35:35 +04:00
channel.queue_bind(exchange=exchange_name, queue='example_queue')
2023-12-04 22:18:07 +04:00
channel.basic_consume(queue='example_queue', on_message_callback=callback, auto_ack=True)
2023-12-05 12:35:35 +04:00
print('Врач 1 ожидает приема')
2023-12-04 22:18:07 +04:00
try:
channel.start_consuming()
except KeyboardInterrupt:
print('Прервано. Останавливаем прием...')
channel.stop_consuming()
connection.close()
2023-12-05 12:35:35 +04:00
2023-12-04 22:18:07 +04:00
if __name__ == '__main__':
consume_messages()