diff --git a/6_tasks/6_14_111_exceptions.py b/6_tasks/6_14_111_exceptions.py new file mode 100644 index 0000000..6fd4d39 --- /dev/null +++ b/6_tasks/6_14_111_exceptions.py @@ -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) diff --git a/7_async_constructions/7_1_070_task.py b/7_async_constructions/7_1_070_task.py new file mode 100644 index 0000000..54d08e9 --- /dev/null +++ b/7_async_constructions/7_1_070_task.py @@ -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()) diff --git a/7_async_constructions/7_2_060_task.py b/7_async_constructions/7_2_060_task.py new file mode 100644 index 0000000..de177ba --- /dev/null +++ b/7_async_constructions/7_2_060_task.py @@ -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)} diff --git a/7_async_constructions/7_2_070_task.py b/7_async_constructions/7_2_070_task.py new file mode 100644 index 0000000..1a9135e --- /dev/null +++ b/7_async_constructions/7_2_070_task.py @@ -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())