DAS_2023_1/gusev_vladislav_lab_4/publisher.py
2023-12-13 15:01:25 +04:00

25 lines
786 B
Python

import pika
import time
import random
def publish_event(channel, exchange_name):
events = ["Пришла фотография", "Пришло сообщение", "Необходимо обработать запрос"]
while True:
event = random.choice(events)
channel.basic_publish(exchange=exchange_name, routing_key='', body=event)
print(f"Отправлено событие: {event}")
time.sleep(3)
def main():
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
exchange_name = 'logs'
channel.exchange_declare(exchange=exchange_name, exchange_type='fanout')
publish_event(channel, exchange_name)
connection.close()
if __name__ == '__main__':
main()