DAS_2023_1/antonov_dmitry_lab4/rabbitmq/publisher.py

20 lines
737 B
Python
Raw Normal View History

2023-12-04 22:18:07 +04:00
import pika
import time
2023-12-05 12:35:35 +04:00
def publish_messages():
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'
channel.exchange_declare(exchange=exchange_name, exchange_type='fanout')
2023-12-04 22:18:07 +04:00
channel.queue_declare(queue='example_queue')
# Отдаем сообщение в 'logs' exchange каждую секунду
while True:
message = "Пациент прибыл" # Сообщение
channel.basic_publish(exchange='logs', routing_key='', body=message)
2023-12-05 12:35:35 +04:00
print(f"Отправлено сообщение: {message}")
2023-12-04 22:18:07 +04:00
time.sleep(1)
2023-12-05 12:35:35 +04:00
print("Отправлено '{}'".format(message))
2023-12-04 22:18:07 +04:00
connection.close()