From ca9fd45ea1685b0be27f511148e4221baed32553 Mon Sep 17 00:00:00 2001 From: pro100ton Date: Tue, 19 Nov 2024 23:38:11 +0300 Subject: [PATCH] Add 6.12 theory files --- 6_tasks/6_12_1_callback_theory.py | 22 ++++++++++++++++++++++ 6_tasks/6_12_callback_theory.py | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 6_tasks/6_12_1_callback_theory.py create mode 100644 6_tasks/6_12_callback_theory.py diff --git a/6_tasks/6_12_1_callback_theory.py b/6_tasks/6_12_1_callback_theory.py new file mode 100644 index 0000000..8ea3f13 --- /dev/null +++ b/6_tasks/6_12_1_callback_theory.py @@ -0,0 +1,22 @@ +import asyncio + + +async def async_operation(): + print("Асинхронная операция началась...") + await asyncio.sleep(2) + print("Асинхронная операция завершена.") + return "Результат асинхронной операции" + + +def on_completion(task): # Callback-функция + result = task.result() + print(f"Callback функция вызвана. Получен результат: {result}") + + +async def main(): + task = asyncio.create_task(async_operation()) + task.add_done_callback(on_completion) # Регистрация callback-функции + await task + + +asyncio.run(main()) diff --git a/6_tasks/6_12_callback_theory.py b/6_tasks/6_12_callback_theory.py new file mode 100644 index 0000000..1c83587 --- /dev/null +++ b/6_tasks/6_12_callback_theory.py @@ -0,0 +1,22 @@ +from typing import Callable + + +def greeting(name): + return f"Привет, {name}!" + + +def farewell(name): + return f"Пока, {name}!" + + +def process_name(callback: Callable, name: str): + return callback(name) + + +name = "Студент" + +greeting_result = process_name(greeting, name) +print(greeting_result) + +farewell_result = process_name(farewell, name) +print(farewell_result)