degtyarev_mikhail_lab_2 is ready #98

Merged
Alexey merged 1 commits from degtyarev_mikhail_lab_2 into main 2024-01-09 11:31:44 +04:00
14 changed files with 172 additions and 0 deletions
Showing only changes of commit e582711076 - Show all commits

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

View File

@ -0,0 +1,66 @@
# Лабораторная 2
## Вариант 9
## Задание
Разработать два приложения такие, что результат первого является исходными данными для второго.
Описание задач двух программ:
- Берёт из каталога /var/data случайный файл и перекладывает его в /var/result/data.txt.
- Ищет наибольшее число из файла /var/data/data.txt и сохраняет его вторую степень в /var/result/result.txt.
## Описание Программы
### Как запустить?
```docker compose up -d```
### Созданные файлы:
**Dockerfile**
Они идентичны
```
FROM python:3.11
WORKDIR /app
COPY . .
CMD ["python", "worker_1.py"]
```
**docker-compose**
```
version: "3"
services:
worker1:
build:
context: ./worker1
dockerfile: Dockerfile
volumes:
- ./data:/var/data
- ./result:/var/result
worker2:
depends_on:
- worker1
build:
context: ./worker2
dockerfile: Dockerfile
volumes:
- ./result:/var/data
- ./result:/var/result
```
**Исходные файлы**
Файлы содержат абсолютно рандомные числа:
![](1.png)
### Результаты
![](2.png)
![](3.png)
Ссылка на видео: https://vk.com/video/@id151119348?z=video151119348_456239199%2Fln-K8jegCHykzxSWCSO4A%2Fpl_151119348_-2

View File

@ -0,0 +1,3 @@
163
446
769

View File

@ -0,0 +1,4 @@
234
125
664
889

View File

@ -0,0 +1,4 @@
1111
522
333
531

View File

@ -0,0 +1,19 @@
version: "3"
services:
worker1:
build:
context: ./worker1
dockerfile: Dockerfile
volumes:
- ./data:/var/data
- ./result:/var/result
worker2:
depends_on:
- worker1
build:
context: ./worker2
dockerfile: Dockerfile
volumes:
- ./result:/var/data
- ./result:/var/result

View File

@ -0,0 +1,4 @@
1111
522
333
531

View File

@ -0,0 +1 @@
1234321

View File

@ -0,0 +1,4 @@
FROM python:3.11
WORKDIR /app
COPY . .
CMD ["python", "worker_1.py"]

View File

@ -0,0 +1,33 @@
import os
import shutil
import random
import logging
def find_random_file(directory):
files = os.listdir(directory)
if not files:
return None
random_file = random.choice(files)
return random_file
def main():
data_dir = '/var/data'
result_file = '/var/result/data.txt'
try:
random_file = find_random_file(data_dir)
if random_file:
src_path = os.path.join(data_dir, random_file)
shutil.copy2(src_path, result_file)
logging.info(f'Successfully copied {src_path} to {result_file}')
else:
logging.warning('No files to copy.')
except Exception as e:
logging.error(f'An error occurred: {str(e)}')
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()

View File

@ -0,0 +1,4 @@
FROM python:3.11
WORKDIR /app
COPY . .
CMD ["python", "worker_2.py"]

View File

@ -0,0 +1,30 @@
def find_max_and_save_square(file_path):
try:
with open(file_path, 'r') as file:
numbers = [int(line.strip()) for line in file.readlines()]
if numbers:
max_number = max(numbers)
square = max_number ** 2
with open("/var/result/result.txt", 'w') as output_file:
output_file.write(str(square))
print(f"Наибольшее число: {max_number}, его вторая степень: {max_number}^2 = {square}")
return square
else:
return None
except Exception as e:
return None
def main():
input_file_path = "/var/data/data.txt"
result = find_max_and_save_square(input_file_path)
if result is None:
print("No result to save.")
if __name__ == "__main__":
main()