diff --git a/morozov_vladimir_lab_4/Teacher_student_message/FirstTest.png b/morozov_vladimir_lab_4/Teacher_student_message/FirstTest.png new file mode 100644 index 0000000..60e2f3d Binary files /dev/null and b/morozov_vladimir_lab_4/Teacher_student_message/FirstTest.png differ diff --git a/morozov_vladimir_lab_4/Teacher_student_message/FirstTest_Cool.png b/morozov_vladimir_lab_4/Teacher_student_message/FirstTest_Cool.png new file mode 100644 index 0000000..0d883f8 Binary files /dev/null and b/morozov_vladimir_lab_4/Teacher_student_message/FirstTest_Cool.png differ diff --git a/morozov_vladimir_lab_4/Teacher_student_message/FirstTest_Normal.png b/morozov_vladimir_lab_4/Teacher_student_message/FirstTest_Normal.png new file mode 100644 index 0000000..0df1ba0 Binary files /dev/null and b/morozov_vladimir_lab_4/Teacher_student_message/FirstTest_Normal.png differ diff --git a/morozov_vladimir_lab_4/Teacher_student_message/SecondTest.png b/morozov_vladimir_lab_4/Teacher_student_message/SecondTest.png new file mode 100644 index 0000000..a2dcfa2 Binary files /dev/null and b/morozov_vladimir_lab_4/Teacher_student_message/SecondTest.png differ diff --git a/morozov_vladimir_lab_4/Teacher_student_message/SecondTestNormal.png b/morozov_vladimir_lab_4/Teacher_student_message/SecondTestNormal.png new file mode 100644 index 0000000..4f2588f Binary files /dev/null and b/morozov_vladimir_lab_4/Teacher_student_message/SecondTestNormal.png differ diff --git a/morozov_vladimir_lab_4/Teacher_student_message/Student_Cool.py b/morozov_vladimir_lab_4/Teacher_student_message/Student_Cool.py new file mode 100644 index 0000000..5f406b3 --- /dev/null +++ b/morozov_vladimir_lab_4/Teacher_student_message/Student_Cool.py @@ -0,0 +1,24 @@ +import pika + +connection = pika.BlockingConnection( + pika.ConnectionParameters(host='localhost')) +channel = connection.channel() +channel.basic_qos(prefetch_count=1) +channel.exchange_declare(exchange='grade_book', exchange_type='fanout') + +result = channel.queue_declare(queue='cool_student', exclusive=True) +queue_name = result.method.queue + +channel.queue_bind(exchange='grade_book', queue=queue_name) + +print(' [*] Waiting for logs. To exit press CTRL+C') + +def callback(ch, method, properties, body): + print(f" [x] Receive: {body}") + print(f" [+] Complete: {body}") + ch.basic_ack(delivery_tag=method.delivery_tag) + +channel.basic_consume( + queue=queue_name, on_message_callback=callback) + +channel.start_consuming() \ No newline at end of file diff --git a/morozov_vladimir_lab_4/Teacher_student_message/Student_Normal.py b/morozov_vladimir_lab_4/Teacher_student_message/Student_Normal.py new file mode 100644 index 0000000..20d3e1b --- /dev/null +++ b/morozov_vladimir_lab_4/Teacher_student_message/Student_Normal.py @@ -0,0 +1,31 @@ +import random +import time + +import pika + +connection = pika.BlockingConnection( + pika.ConnectionParameters(host='localhost')) +channel = connection.channel() + +channel.basic_qos(prefetch_count=1) + +channel.exchange_declare(exchange='grade_book', exchange_type='fanout') + +result = channel.queue_declare(queue='normal_student', durable=True) + +queue_name = result.method.queue + +channel.queue_bind(exchange='grade_book', queue=queue_name) + +print(' [*] Waiting for tasks. To exit press CTRL+C') + +def callback(ch, method, properties, body): + print(f" [x] Receive: {body}") + time.sleep(random.randint(2,4)) + print(f" [+] Complete: {body}") + ch.basic_ack(delivery_tag=method.delivery_tag) + +channel.basic_consume( + queue=queue_name, on_message_callback=callback) + +channel.start_consuming() \ No newline at end of file diff --git a/morozov_vladimir_lab_4/Teacher_student_message/teacher.py b/morozov_vladimir_lab_4/Teacher_student_message/teacher.py new file mode 100644 index 0000000..6d55c8e --- /dev/null +++ b/morozov_vladimir_lab_4/Teacher_student_message/teacher.py @@ -0,0 +1,25 @@ +import sys +import time + +import pika + +connection = pika.BlockingConnection( + pika.ConnectionParameters(host='localhost')) +channel = connection.channel() + +channel.exchange_declare(exchange='grade_book', + exchange_type='fanout') + +count = 0 +while True: + count+= 1 + time.sleep(1) + message = f"Homework #{count}" + channel.basic_publish(exchange='grade_book', + routing_key='', + body=message, + properties=pika.BasicProperties( + delivery_mode=pika.DeliveryMode.Persistent + )) + print(f" [x] Sent {message}") + diff --git a/morozov_vladimir_lab_4/lesson_1/img.png b/morozov_vladimir_lab_4/lesson_1/img.png new file mode 100644 index 0000000..6c103ae Binary files /dev/null and b/morozov_vladimir_lab_4/lesson_1/img.png differ diff --git a/morozov_vladimir_lab_4/lesson_1/receive.py b/morozov_vladimir_lab_4/lesson_1/receive.py new file mode 100644 index 0000000..f118e0a --- /dev/null +++ b/morozov_vladimir_lab_4/lesson_1/receive.py @@ -0,0 +1,25 @@ +import pika, sys, os + +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) \ No newline at end of file diff --git a/morozov_vladimir_lab_4/lesson_1/send.py b/morozov_vladimir_lab_4/lesson_1/send.py new file mode 100644 index 0000000..41cfff2 --- /dev/null +++ b/morozov_vladimir_lab_4/lesson_1/send.py @@ -0,0 +1,11 @@ +import pika + +connection = pika.BlockingConnection( + pika.ConnectionParameters(host='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() \ No newline at end of file diff --git a/morozov_vladimir_lab_4/lesson_2/img.png b/morozov_vladimir_lab_4/lesson_2/img.png new file mode 100644 index 0000000..87f5a27 Binary files /dev/null and b/morozov_vladimir_lab_4/lesson_2/img.png differ diff --git a/morozov_vladimir_lab_4/lesson_2/new_task.py b/morozov_vladimir_lab_4/lesson_2/new_task.py new file mode 100644 index 0000000..c87ac4f --- /dev/null +++ b/morozov_vladimir_lab_4/lesson_2/new_task.py @@ -0,0 +1,18 @@ +import sys +import pika + +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}") \ No newline at end of file diff --git a/morozov_vladimir_lab_4/lesson_2/worker.py b/morozov_vladimir_lab_4/lesson_2/worker.py new file mode 100644 index 0000000..d6c601d --- /dev/null +++ b/morozov_vladimir_lab_4/lesson_2/worker.py @@ -0,0 +1,29 @@ +import time + +import pika, sys, os + +def main(): + connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost')) + channel = connection.channel() + channel.basic_qos(prefetch_count=1) + channel.queue_declare(queue='task_queue', durable=True) + 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_consume(queue='task_queue', on_message_callback=callback) + + 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) \ No newline at end of file diff --git a/morozov_vladimir_lab_4/lesson_3/img.png b/morozov_vladimir_lab_4/lesson_3/img.png new file mode 100644 index 0000000..535b0a5 Binary files /dev/null and b/morozov_vladimir_lab_4/lesson_3/img.png differ diff --git a/morozov_vladimir_lab_4/lesson_3/receiver.py b/morozov_vladimir_lab_4/lesson_3/receiver.py new file mode 100644 index 0000000..60d881d --- /dev/null +++ b/morozov_vladimir_lab_4/lesson_3/receiver.py @@ -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() \ No newline at end of file diff --git a/morozov_vladimir_lab_4/lesson_3/send.py b/morozov_vladimir_lab_4/lesson_3/send.py new file mode 100644 index 0000000..4b9d7c1 --- /dev/null +++ b/morozov_vladimir_lab_4/lesson_3/send.py @@ -0,0 +1,20 @@ +import sys +import pika + +connection = pika.BlockingConnection( + pika.ConnectionParameters(host='localhost')) +channel = connection.channel() + +channel.exchange_declare(exchange='logs', + exchange_type='fanout') + +message = ' '.join(sys.argv[1:]) or "Hello World!" + +channel.basic_publish(exchange='logs', + routing_key='', + body=message, + properties=pika.BasicProperties( + delivery_mode=pika.DeliveryMode.Persistent + )) + +print(f" [x] Sent {message}") \ No newline at end of file diff --git a/morozov_vladimir_lab_4/readme.md b/morozov_vladimir_lab_4/readme.md new file mode 100644 index 0000000..f027475 --- /dev/null +++ b/morozov_vladimir_lab_4/readme.md @@ -0,0 +1,44 @@ +# Лабораторная работа №4 - Работа с брокером сообщений + +## Прохождение уроков + +Урок №1. +![img.png](lesson_1%2Fimg.png) + +Урок №2. +![img.png](lesson_2%2Fimg.png) + +Урок №3. +![img.png](lesson_3%2Fimg.png) + +Все приложения были разработаны на Python + +## Разработанные приложения +В качестве предметной области было выбрано общение учителя и его учеников. Учитель дает задание ученикам, а они их выполняют. Я выделил 2 вида учеников: обычные, которым нужно некоторое время на то, чтобы выполнить задание; крутые ученики, которые выполняют задание моментально, как только получают его. +Были созданы следующие приложения: +- teacher - программа, которая представляет учителя и отправляет задания ученикам +- Student_Normal - программа, которая представялет обычного ученика, получает и обрабатывает, с задержкой, полученные задания. +- Student_Cool - программа, которая представялет крутого ученика, получает и моментально обрабатывает полученные задания. +Все программы были разработаны на языке Python. +Проведенные тесты: +#### Тест №1. Запущены 1 учитель, 1 обычный ученик, 1 крутой ученик: +![FirstTest.png](Teacher_student_message%2FFirstTest.png) +Показатели очереди обычного ученика: +![FirstTest_Normal.png](Teacher_student_message%2FFirstTest_Normal.png) +Показатели очереди крутого ученика: +![FirstTest_Cool.png](Teacher_student_message%2FFirstTest_Cool.png) + +Вывод: одного обычного ученика не хватает на то, чтобы выполнять все полученные в срок, ему тяжело :( +Крутому же ученику все дается с легкостью. + +#### Тест №2. Запущены 1 учитель, 4 обычных ученика: +![SecondTest.png](Teacher_student_message%2FSecondTest.png) +Показатели очереди обычного ученика: +![FirstTest_Normal.png](Teacher_student_message%2FFirstTest_Normal.png) +Вывод: если 4 обычных ученика объединяться и будут делать задания вместе, то они смогут избежать переполнения очереди заданий. + + + + +## Запись тестирования +Работа приложения представлена в [видео](https://disk.yandex.ru/i/zzwvXXpZhavh7A) \ No newline at end of file