forked from Alexey/DAS_2024_1
mochalov_danila_lab_4 is ready
This commit is contained in:
parent
3b9698ac38
commit
f0b4e6d172
33
mochalov_danila_lab_4/labwork/consumer_1.py
Normal file
33
mochalov_danila_lab_4/labwork/consumer_1.py
Normal file
@ -0,0 +1,33 @@
|
||||
import pika
|
||||
import time
|
||||
|
||||
|
||||
def callback(ch, method, properties, body):
|
||||
print(f'CONSUMER 1 ПОЛУЧИЛ СООБЩЕНИЕ: {body.decode()}')
|
||||
|
||||
time.sleep(3)
|
||||
|
||||
print(f'CONSUMER 1 ОБРАБОТАЛ СООБЩЕНИЕ: {body.decode()}')
|
||||
|
||||
|
||||
def consume():
|
||||
connection = pika.BlockingConnection(
|
||||
pika.ConnectionParameters(
|
||||
host='localhost',
|
||||
port=5672
|
||||
)
|
||||
)
|
||||
|
||||
channel = connection.channel()
|
||||
|
||||
channel.queue_declare(queue='consumer_1_queue')
|
||||
channel.queue_bind(exchange='game_exchange', queue='consumer_1_queue')
|
||||
|
||||
channel.basic_consume(queue='consumer_1_queue', on_message_callback=callback, auto_ack=True)
|
||||
|
||||
print('CONSUMER 1 ОЖИДАЕТ СООБЩЕНИЙ...')
|
||||
channel.start_consuming()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
consume()
|
30
mochalov_danila_lab_4/labwork/consumer_2.py
Normal file
30
mochalov_danila_lab_4/labwork/consumer_2.py
Normal file
@ -0,0 +1,30 @@
|
||||
import pika
|
||||
|
||||
|
||||
def callback(ch, method, properties, body):
|
||||
print(f'CONSUMER 2 ПОЛУЧИЛ СООБЩЕНИЕ: {body.decode()}')
|
||||
|
||||
print(f'CONSUMER 2 ОБРАБОТАЛ СООБЩЕНИЕ: {body.decode()}')
|
||||
|
||||
|
||||
def consume():
|
||||
connection = pika.BlockingConnection(
|
||||
pika.ConnectionParameters(
|
||||
host='localhost',
|
||||
port=5672
|
||||
)
|
||||
)
|
||||
|
||||
channel = connection.channel()
|
||||
|
||||
channel.queue_declare(queue='consumer_2_queue')
|
||||
channel.queue_bind(exchange='game_exchange', queue='consumer_2_queue')
|
||||
|
||||
channel.basic_consume(queue='consumer_2_queue', on_message_callback=callback, auto_ack=True)
|
||||
|
||||
print('CONSUMER 2 ОЖИДАЕТ СООБЩЕНИЙ...')
|
||||
channel.start_consuming()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
consume()
|
29
mochalov_danila_lab_4/labwork/producer.py
Normal file
29
mochalov_danila_lab_4/labwork/producer.py
Normal file
@ -0,0 +1,29 @@
|
||||
import pika
|
||||
import time
|
||||
|
||||
def produce():
|
||||
connection = pika.BlockingConnection(
|
||||
pika.ConnectionParameters(
|
||||
host='localhost',
|
||||
port=5672
|
||||
)
|
||||
)
|
||||
channel = connection.channel()
|
||||
|
||||
channel.exchange_declare(exchange='game_exchange', exchange_type='fanout')
|
||||
|
||||
messages = [
|
||||
"Вышла новая игра: Brotato",
|
||||
"Ваш друг играет в Cult of the Lamb",
|
||||
"Ваша игра из списка желаемого продается со скидкой!"
|
||||
]
|
||||
|
||||
while True:
|
||||
for message in messages:
|
||||
channel.basic_publish(exchange='game_exchange', routing_key='', body=message)
|
||||
print(f'Отправлено: {message}')
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
produce()
|
41
mochalov_danila_lab_4/readme.md
Normal file
41
mochalov_danila_lab_4/readme.md
Normal file
@ -0,0 +1,41 @@
|
||||
# Лабораторная работа №4
|
||||
#### ПИбд-42. Мочалов Данила.
|
||||
|
||||
#### Выполнение туториала
|
||||
|
||||
- Первый
|
||||
![Скриншот 1](./screenshots/tutorial_1.png)
|
||||
|
||||
- Второй
|
||||
![Скриншот 2](./screenshots/tutorial_2.png)
|
||||
|
||||
- Третий
|
||||
![Скриншот 3](./screenshots/tutorial_3.png)
|
||||
|
||||
#### Выполнение лабораторной работы
|
||||
|
||||
Выбранная предметная область: игровой лаунчер (а-ля Steam)
|
||||
|
||||
Сначала попробовал запустить продюсера, первого и второго консьюмера в одном экземпляре.
|
||||
|
||||
Результат:
|
||||
![Скриншот 4](./screenshots/labwork_consumer_1_and_consumer_2.png)
|
||||
|
||||
Вывод:
|
||||
Очередь первого консьюмера быстро переполняется, так как скорость поступления сообщений в очередь больше чем скорость обработки сообщений первым консьюмером.
|
||||
Очередь второго коньюмера никогда не заполняется, так как он моментально обрабатывает все входящие сообщения.
|
||||
|
||||
Теперь запускаю сначала один экземпляр первого консьюмера, а потом несколько, чтобы они разгрузили накопившуюся очередь.
|
||||
|
||||
Результат:
|
||||
![Скриншот 5](./screenshots/labwork_consumers_1_only.png)
|
||||
|
||||
Вывод:
|
||||
Накопившаяся очередь быстро разгружается благодаря нескольким одновременно запущенным экземплярам первого консьюмера.
|
||||
|
||||
#### Демонстрация работы
|
||||
|
||||
На видео я сначала показываю как сильно загружается очередь при одном экземпляре первого консьюмера, потом запускаю еще парочку экземпляров и мы смотрим как очередь разгружается (и радуемся xd).
|
||||
Еще в моменте показываю что второй консьюмер тоже работает отлично.
|
||||
Видео по [ссылке](https://drive.google.com/file/d/1mgCpKw12GJZbYyAeqXKMQG9XbWI5gV_D/view?usp=sharing)
|
||||
|
Binary file not shown.
After Width: | Height: | Size: 125 KiB |
BIN
mochalov_danila_lab_4/screenshots/labwork_consumers_1_only.png
Normal file
BIN
mochalov_danila_lab_4/screenshots/labwork_consumers_1_only.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 45 KiB |
BIN
mochalov_danila_lab_4/screenshots/tutorial_1.png
Normal file
BIN
mochalov_danila_lab_4/screenshots/tutorial_1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 30 KiB |
BIN
mochalov_danila_lab_4/screenshots/tutorial_2.png
Normal file
BIN
mochalov_danila_lab_4/screenshots/tutorial_2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 48 KiB |
BIN
mochalov_danila_lab_4/screenshots/tutorial_3.png
Normal file
BIN
mochalov_danila_lab_4/screenshots/tutorial_3.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 30 KiB |
13
mochalov_danila_lab_4/tutorial_1/producer.py
Normal file
13
mochalov_danila_lab_4/tutorial_1/producer.py
Normal file
@ -0,0 +1,13 @@
|
||||
import pika
|
||||
|
||||
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.queue_declare(queue='hello')
|
||||
|
||||
channel.basic_publish(exchange='',
|
||||
routing_key='hello',
|
||||
body='Hello World!')
|
||||
print(" [x] Sent 'Hello World!'")
|
||||
|
||||
connection.close()
|
28
mochalov_danila_lab_4/tutorial_1/receiver.py
Normal file
28
mochalov_danila_lab_4/tutorial_1/receiver.py
Normal file
@ -0,0 +1,28 @@
|
||||
import os
|
||||
import sys
|
||||
import pika
|
||||
|
||||
|
||||
def main():
|
||||
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.queue_declare(queue='hello')
|
||||
|
||||
def callback(ch, method, properties, body):
|
||||
print(f" [x] Received {body}")
|
||||
|
||||
channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)
|
||||
|
||||
print(' [*] Waiting for messages. To exit press CTRL+C')
|
||||
channel.start_consuming()
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except KeyboardInterrupt:
|
||||
print('Interrupted')
|
||||
try:
|
||||
sys.exit(0)
|
||||
except SystemExit:
|
||||
os._exit(0)
|
19
mochalov_danila_lab_4/tutorial_2/new_task.py
Normal file
19
mochalov_danila_lab_4/tutorial_2/new_task.py
Normal file
@ -0,0 +1,19 @@
|
||||
import pika
|
||||
import sys
|
||||
|
||||
connection = pika.BlockingConnection(
|
||||
pika.ConnectionParameters(host='localhost'))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.queue_declare(queue='task_queue', durable=True)
|
||||
|
||||
message = ' '.join(sys.argv[1:]) or "Hello World!"
|
||||
channel.basic_publish(
|
||||
exchange='',
|
||||
routing_key='task_queue',
|
||||
body=message,
|
||||
properties=pika.BasicProperties(
|
||||
delivery_mode=pika.DeliveryMode.Persistent
|
||||
))
|
||||
print(f" [x] Sent {message}")
|
||||
connection.close()
|
22
mochalov_danila_lab_4/tutorial_2/worker.py
Normal file
22
mochalov_danila_lab_4/tutorial_2/worker.py
Normal file
@ -0,0 +1,22 @@
|
||||
import pika
|
||||
import time
|
||||
|
||||
connection = pika.BlockingConnection(
|
||||
pika.ConnectionParameters(host='localhost'))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.queue_declare(queue='task_queue', durable=True)
|
||||
print(' [*] Waiting for messages. To exit press CTRL+C')
|
||||
|
||||
|
||||
def callback(ch, method, properties, body):
|
||||
print(f" [x] Received {body.decode()}")
|
||||
time.sleep(body.count(b'.'))
|
||||
print(" [x] Done")
|
||||
ch.basic_ack(delivery_tag=method.delivery_tag)
|
||||
|
||||
|
||||
channel.basic_qos(prefetch_count=1)
|
||||
channel.basic_consume(queue='task_queue', on_message_callback=callback)
|
||||
|
||||
channel.start_consuming()
|
13
mochalov_danila_lab_4/tutorial_3/emit_log.py
Normal file
13
mochalov_danila_lab_4/tutorial_3/emit_log.py
Normal file
@ -0,0 +1,13 @@
|
||||
import pika
|
||||
import sys
|
||||
|
||||
connection = pika.BlockingConnection(
|
||||
pika.ConnectionParameters(host='localhost'))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.exchange_declare(exchange='logs', exchange_type='fanout')
|
||||
|
||||
message = ' '.join(sys.argv[1:]) or "info: Hello World!"
|
||||
channel.basic_publish(exchange='logs', routing_key='', body=message)
|
||||
print(f" [x] Sent {message}")
|
||||
connection.close()
|
22
mochalov_danila_lab_4/tutorial_3/receive_logs.py
Normal file
22
mochalov_danila_lab_4/tutorial_3/receive_logs.py
Normal file
@ -0,0 +1,22 @@
|
||||
import pika
|
||||
|
||||
connection = pika.BlockingConnection(
|
||||
pika.ConnectionParameters(host='localhost'))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.exchange_declare(exchange='logs', exchange_type='fanout')
|
||||
|
||||
result = channel.queue_declare(queue='', exclusive=True)
|
||||
queue_name = result.method.queue
|
||||
|
||||
channel.queue_bind(exchange='logs', queue=queue_name)
|
||||
|
||||
print(' [*] Waiting for logs. To exit press CTRL+C')
|
||||
|
||||
def callback(ch, method, properties, body):
|
||||
print(f" [x] {body}")
|
||||
|
||||
channel.basic_consume(
|
||||
queue=queue_name, on_message_callback=callback, auto_ack=True)
|
||||
|
||||
channel.start_consuming()
|
Loading…
Reference in New Issue
Block a user