DAS_2023_1/savenkov_alexander_lab_4/publisher.py

27 lines
903 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pika
import time
import random
def generate_order_event(channel):
order_id = random.randint(1, 1000)
event = f"Поступил новый заказ #{order_id}"
message = f"Событие: {event}"
channel.basic_publish(exchange='events', routing_key='', body=message)
print(f" [x] Sent: {message}")
def main():
# Устанавливаем соединение с сервером RabbitMQ
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
# Объявляем exchange с типом 'fanout'
channel.exchange_declare(exchange='events', exchange_type='fanout')
# В бесконечном цикле генерируем и отправляем события в RabbitMQ
while True:
generate_order_event(channel)
time.sleep(1)
if __name__ == '__main__':
main()