DAS_2023_1/antonov_dmitry_lab4/rabbitmq/consumer2.py

27 lines
830 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("Врач 2 принимает пациента '{}'".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)
print(' Врач 2 ожидает пациента')
try:
channel.start_consuming()
except KeyboardInterrupt:
print('Врач 2 прекращает прием')
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()