Add final solutions for 6 an 7 secitions alltogether

This commit is contained in:
pro100ton 2024-12-04 21:57:42 +03:00
parent 8b391c9845
commit c8d41e5fed
4 changed files with 92 additions and 0 deletions

View file

@ -0,0 +1,17 @@
import asyncio
from asyncio.exceptions import CancelledError
files = ["image.png", "file.csv", "file1.txt"]
async def download_file(file_name):
pass
async def main():
try:
async with asyncio.TaskGroup() as tg:
tasks = [tg.create_task(download_file(filename)) for filename in files]
except* Exception as err:
for error in err.exceptions:
print(error)

View file

@ -0,0 +1,42 @@
import asyncio
# База данных
database = [
{"название": "Разработать API", "статус": "Завершена"},
{"название": "Написать документацию", "статус": "Ожидает"},
{"название": "Провести код-ревью", "статус": "Ожидает"},
]
# Не менять
class AsyncListManager:
async def __aenter__(self):
await self.connect()
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.disconnect()
async def connect(self):
print("Начало работы с базой данных")
await asyncio.sleep(0.5)
async def disconnect(self):
print("Завершение работы с базой данных")
await asyncio.sleep(0.5)
async def stage_append(self, value):
await asyncio.sleep(1)
database.append(value)
print("Новые данные добавлены")
# Тут пишите ваш код
async def main():
async with AsyncListManager() as db_manager:
await db_manager.stage_append({'название': 'Настроить CI/CD', 'статус': 'В процессе'})
for db in database:
print(db)
asyncio.run(main())

View file

@ -0,0 +1,8 @@
import random
users = ['user1', 'user2', 'user3']
products = ['iPhone 14', 'Samsung Galaxy S23', 'MacBook Pro', 'Dell XPS 13', 'Sony WH-1000XM5', 'Apple Watch Series 8', 'Kindle Paperwhite', 'GoPro Hero 11', 'Nintendo Switch', 'Oculus Quest 2']
actions = ['просмотр', 'покупка', 'добавление в избранное']
async def user_action_generator():
yield {'user_id': random.choice(users), 'action': random.choice(actions), 'product_id': random.choice(products)}

View file

@ -0,0 +1,25 @@
import asyncio
import random
random.seed(1)
SERVERS = [
"api.database.local",
"auth.backend.local",
"web.frontend.local",
"cache.redis.local",
"analytics.bigdata.local"
]
STATUSES = ["Online", "Offline", "Maintenance", "Error"]
async def monitor_servers(servers):
for server in servers:
await asyncio.sleep(.1)
yield (server, random.choice(STATUSES))
async def main():
async for server_name, server_status in monitor_servers(SERVERS):
print(f'{server_name}: состояние {server_status}')
asyncio.run(main())