Compare commits
2 Commits
8ad85b8049
...
915b8bb68c
Author | SHA1 | Date | |
---|---|---|---|
915b8bb68c | |||
|
7ea3ef3097 |
115
tasks/ostrovskaya-sf/lab_4/README.md
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
# Отчет по лабораторной работе №4
|
||||||
|
|
||||||
|
Выполнила студентка гр. ИСЭбд-41 Островская С. Ф.
|
||||||
|
|
||||||
|
## Прохождение tutorial
|
||||||
|
|
||||||
|
Установила rabbitMQ server, erlang и зашла в брокер под гостем по http://localhost:15672/#/
|
||||||
|
![](pic/pic1.jpg)
|
||||||
|
|
||||||
|
Туториал 1:
|
||||||
|
|
||||||
|
![](pic/pic2.jpg)
|
||||||
|
![](pic/pic3.jpg)
|
||||||
|
|
||||||
|
Туториал 2:
|
||||||
|
|
||||||
|
![](pic/pic4.jpg)
|
||||||
|
![](pic/pic5.jpg)
|
||||||
|
|
||||||
|
Туториал 3:
|
||||||
|
|
||||||
|
![](pic/pic6.jpg)
|
||||||
|
![](pic/pic7.jpg)
|
||||||
|
|
||||||
|
## Разработка демонстрационных приложений
|
||||||
|
|
||||||
|
Предметная область: Дополнительный соглашения и договоры на проживание.
|
||||||
|
Разработаны три приложения, согласно предметной области.
|
||||||
|
|
||||||
|
1. Publisher
|
||||||
|
|
||||||
|
```py
|
||||||
|
import pika
|
||||||
|
import time
|
||||||
|
import random
|
||||||
|
|
||||||
|
collection = ["Application for accommodation in a dormitory", "Rector's order", "Conclusion of an agreement", "Check-into a dormitory"]
|
||||||
|
|
||||||
|
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
|
||||||
|
channel = connection.channel()
|
||||||
|
|
||||||
|
channel.exchange_declare(exchange='Agreements', exchange_type='fanout')
|
||||||
|
|
||||||
|
while True:
|
||||||
|
message = f"Message: {random.choice(collection)}"
|
||||||
|
|
||||||
|
channel.basic_publish(exchange='Agreements', routing_key='', body=message)
|
||||||
|
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
connection.close()
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Consumer 1.
|
||||||
|
|
||||||
|
```py
|
||||||
|
import pika
|
||||||
|
import time
|
||||||
|
|
||||||
|
def process_message(ch, method, properties, body):
|
||||||
|
print(f"Получено сообщение: {body}")
|
||||||
|
time.sleep(3)
|
||||||
|
print("Сообщение успешно обработано")
|
||||||
|
|
||||||
|
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
|
||||||
|
channel = connection.channel()
|
||||||
|
|
||||||
|
result = channel.queue_declare(queue='', exclusive=True)
|
||||||
|
queue_name = result.method.queue
|
||||||
|
|
||||||
|
channel.queue_bind(exchange='Agreements', queue=queue_name)
|
||||||
|
|
||||||
|
channel.basic_consume(queue=queue_name, on_message_callback=process_message, auto_ack=True)
|
||||||
|
|
||||||
|
print('Ожидание сообщений...')
|
||||||
|
channel.start_consuming()
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Consumer 2.
|
||||||
|
|
||||||
|
```py
|
||||||
|
import pika
|
||||||
|
|
||||||
|
|
||||||
|
def process_message(ch, method, properties, body):
|
||||||
|
print(f"Получено сообщение: {body}")
|
||||||
|
print("Сообщение успешно обработано")
|
||||||
|
|
||||||
|
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
|
||||||
|
channel = connection.channel()
|
||||||
|
|
||||||
|
result = channel.queue_declare(queue='', exclusive=True)
|
||||||
|
queue_name = result.method.queue
|
||||||
|
|
||||||
|
channel.queue_bind(exchange='Agreements', queue=queue_name)
|
||||||
|
|
||||||
|
channel.basic_consume(queue=queue_name, on_message_callback=process_message, auto_ack=True)
|
||||||
|
|
||||||
|
print('Ожидание сообщений...')
|
||||||
|
channel.start_consuming()
|
||||||
|
```
|
||||||
|
|
||||||
|
## Результаты выполнения лабораторной работы
|
||||||
|
|
||||||
|
Результат отработки Consumer_1:
|
||||||
|
|
||||||
|
![](pic/pic8.jpg)
|
||||||
|
![](pic/pic9.jpg)
|
||||||
|
|
||||||
|
Результат отработки Consumer_2:
|
||||||
|
|
||||||
|
![](pic/pic10.jpg)
|
||||||
|
![](pic/pic11.jpg)
|
||||||
|
|
||||||
|
Вывод: Consumer_2 нагружает меньше памяти, чем Consumer_1 и принимает сообщения гораздо быстрее, тем самым не позволяя очереди накапливать огромное количество сообщений.
|
3
tasks/ostrovskaya-sf/lab_4/lab_4_py/.idea/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
@ -0,0 +1,6 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<settings>
|
||||||
|
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||||
|
<version value="1.0" />
|
||||||
|
</settings>
|
||||||
|
</component>
|
8
tasks/ostrovskaya-sf/lab_4/lab_4_py/.idea/lab_4_py.iml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="PYTHON_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
4
tasks/ostrovskaya-sf/lab_4/lab_4_py/.idea/misc.xml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
|
||||||
|
</project>
|
8
tasks/ostrovskaya-sf/lab_4/lab_4_py/.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/lab_4_py.iml" filepath="$PROJECT_DIR$/.idea/lab_4_py.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
20
tasks/ostrovskaya-sf/lab_4/lab_4_py/main_task/consumer_1.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import pika
|
||||||
|
import time
|
||||||
|
|
||||||
|
def process_message(ch, method, properties, body):
|
||||||
|
print(f"Получено сообщение: {body}")
|
||||||
|
time.sleep(3)
|
||||||
|
print("Сообщение успешно обработано")
|
||||||
|
|
||||||
|
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
|
||||||
|
channel = connection.channel()
|
||||||
|
|
||||||
|
result = channel.queue_declare(queue='', exclusive=True)
|
||||||
|
queue_name = result.method.queue
|
||||||
|
|
||||||
|
channel.queue_bind(exchange='Agreements', queue=queue_name)
|
||||||
|
|
||||||
|
channel.basic_consume(queue=queue_name, on_message_callback=process_message, auto_ack=True)
|
||||||
|
|
||||||
|
print('Ожидание сообщений...')
|
||||||
|
channel.start_consuming()
|
19
tasks/ostrovskaya-sf/lab_4/lab_4_py/main_task/consumer_2.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import pika
|
||||||
|
|
||||||
|
|
||||||
|
def process_message(ch, method, properties, body):
|
||||||
|
print(f"Получено сообщение: {body}")
|
||||||
|
print("Сообщение успешно обработано")
|
||||||
|
|
||||||
|
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
|
||||||
|
channel = connection.channel()
|
||||||
|
|
||||||
|
result = channel.queue_declare(queue='', exclusive=True)
|
||||||
|
queue_name = result.method.queue
|
||||||
|
|
||||||
|
channel.queue_bind(exchange='Agreements', queue=queue_name)
|
||||||
|
|
||||||
|
channel.basic_consume(queue=queue_name, on_message_callback=process_message, auto_ack=True)
|
||||||
|
|
||||||
|
print('Ожидание сообщений...')
|
||||||
|
channel.start_consuming()
|
19
tasks/ostrovskaya-sf/lab_4/lab_4_py/main_task/publisher.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import pika
|
||||||
|
import time
|
||||||
|
import random
|
||||||
|
|
||||||
|
collection = ["Application for accommodation in a dormitory", "Rector's order", "Conclusion of an agreement", "Check-into a dormitory"]
|
||||||
|
|
||||||
|
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
|
||||||
|
channel = connection.channel()
|
||||||
|
|
||||||
|
channel.exchange_declare(exchange='Agreements', exchange_type='fanout')
|
||||||
|
|
||||||
|
while True:
|
||||||
|
message = f"Message: {random.choice(collection)}"
|
||||||
|
|
||||||
|
channel.basic_publish(exchange='Agreements', routing_key='', body=message)
|
||||||
|
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
connection.close()
|
25
tasks/ostrovskaya-sf/lab_4/lab_4_py/tutorial_1/receive.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import pika, sys, os
|
||||||
|
|
||||||
|
def main():
|
||||||
|
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
|
||||||
|
channel = connection.channel()
|
||||||
|
|
||||||
|
channel.queue_declare(queue='OstrovsofaLogs')
|
||||||
|
|
||||||
|
def callback(ch, method, properties, body):
|
||||||
|
print(f" [x] Получено сообщение: {body}")
|
||||||
|
|
||||||
|
channel.basic_consume(queue='OstrovsofaLogs', on_message_callback=callback, auto_ack=True)
|
||||||
|
|
||||||
|
print(' [*] Ожидание сообщений. Для завершения CTRL+C')
|
||||||
|
channel.start_consuming()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
try:
|
||||||
|
main()
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print('Произошла ошибка')
|
||||||
|
try:
|
||||||
|
sys.exit(0)
|
||||||
|
except SystemExit:
|
||||||
|
os._exit(0)
|
11
tasks/ostrovskaya-sf/lab_4/lab_4_py/tutorial_1/send.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import pika
|
||||||
|
|
||||||
|
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
|
||||||
|
channel = connection.channel()
|
||||||
|
|
||||||
|
channel.queue_declare(queue='ostrovsofa')
|
||||||
|
|
||||||
|
channel.basic_publish(exchange='',routing_key='ostrovsofa',body='My cat"s name is Montik!')
|
||||||
|
print(" [x] Отправлено сообщение")
|
||||||
|
|
||||||
|
connection.close()
|
13
tasks/ostrovskaya-sf/lab_4/lab_4_py/tutorial_2/new_task.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import pika
|
||||||
|
import sys
|
||||||
|
|
||||||
|
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
|
||||||
|
channel = connection.channel()
|
||||||
|
|
||||||
|
channel.queue_declare(queue='ostrovsofa2', durable=True)
|
||||||
|
|
||||||
|
message = ' '.join(sys.argv[1:]) or "My cat's name is Montik!"
|
||||||
|
channel.basic_publish(exchange='', routing_key='ostrovsofa2', body=message, properties=pika.BasicProperties(
|
||||||
|
delivery_mode=pika.spec.PERSISTENT_DELIVERY_MODE))
|
||||||
|
print(f" [x] Отправлено {message}")
|
||||||
|
connection.close()
|
19
tasks/ostrovskaya-sf/lab_4/lab_4_py/tutorial_2/worker.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import pika
|
||||||
|
import time
|
||||||
|
|
||||||
|
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
|
||||||
|
channel = connection.channel()
|
||||||
|
|
||||||
|
channel.queue_declare(queue='ostrovsofa2', durable=True)
|
||||||
|
print(' [*] Ожидание сообщений. Для завершения CTRL+C')
|
||||||
|
|
||||||
|
def callback(ch, method, properties, body):
|
||||||
|
print(f" [x] Получено сообщение: {body.decode()}")
|
||||||
|
time.sleep(body.count(b'.'))
|
||||||
|
print(" [x] Выполнено")
|
||||||
|
ch.basic_ack(delivery_tag=method.delivery_tag)
|
||||||
|
|
||||||
|
channel.basic_qos(prefetch_count=1)
|
||||||
|
channel.basic_consume(queue='ostrovsofa2', on_message_callback=callback)
|
||||||
|
|
||||||
|
channel.start_consuming()
|
12
tasks/ostrovskaya-sf/lab_4/lab_4_py/tutorial_3/emit_log.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import pika
|
||||||
|
import sys
|
||||||
|
|
||||||
|
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
|
||||||
|
channel = connection.channel()
|
||||||
|
|
||||||
|
channel.exchange_declare(exchange='OstrovsofaLogs', exchange_type='fanout')
|
||||||
|
|
||||||
|
message = ' '.join(sys.argv[1:]) or "Info: My cat's name is Montik!"
|
||||||
|
channel.basic_publish(exchange='OstrovsofaLogs', routing_key='', body=message)
|
||||||
|
print(f" [x] Отправлено сообщение: {message}")
|
||||||
|
connection.close()
|
@ -0,0 +1,21 @@
|
|||||||
|
import pika
|
||||||
|
|
||||||
|
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
|
||||||
|
channel = connection.channel()
|
||||||
|
|
||||||
|
channel.exchange_declare(exchange='OstrovsofaLogs', exchange_type='fanout')
|
||||||
|
|
||||||
|
result = channel.queue_declare(queue='', exclusive=True)
|
||||||
|
queue_name = result.method.queue
|
||||||
|
|
||||||
|
channel.queue_bind(exchange='OstrovsofaLogs', queue=queue_name)
|
||||||
|
|
||||||
|
print(' [*] Ожидание сообщений. Для завершения 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()
|
BIN
tasks/ostrovskaya-sf/lab_4/pic/pic1.jpg
Normal file
After Width: | Height: | Size: 26 KiB |
BIN
tasks/ostrovskaya-sf/lab_4/pic/pic10.jpg
Normal file
After Width: | Height: | Size: 76 KiB |
BIN
tasks/ostrovskaya-sf/lab_4/pic/pic11.jpg
Normal file
After Width: | Height: | Size: 132 KiB |
BIN
tasks/ostrovskaya-sf/lab_4/pic/pic2.jpg
Normal file
After Width: | Height: | Size: 53 KiB |
BIN
tasks/ostrovskaya-sf/lab_4/pic/pic3.jpg
Normal file
After Width: | Height: | Size: 87 KiB |
BIN
tasks/ostrovskaya-sf/lab_4/pic/pic4.jpg
Normal file
After Width: | Height: | Size: 41 KiB |
BIN
tasks/ostrovskaya-sf/lab_4/pic/pic5.jpg
Normal file
After Width: | Height: | Size: 124 KiB |
BIN
tasks/ostrovskaya-sf/lab_4/pic/pic6.jpg
Normal file
After Width: | Height: | Size: 34 KiB |
BIN
tasks/ostrovskaya-sf/lab_4/pic/pic7.jpg
Normal file
After Width: | Height: | Size: 127 KiB |
BIN
tasks/ostrovskaya-sf/lab_4/pic/pic8.jpg
Normal file
After Width: | Height: | Size: 74 KiB |
BIN
tasks/ostrovskaya-sf/lab_4/pic/pic9.jpg
Normal file
After Width: | Height: | Size: 130 KiB |