Add 6.12 theory files
This commit is contained in:
parent
0ee735b8a6
commit
ca9fd45ea1
2 changed files with 44 additions and 0 deletions
22
6_tasks/6_12_1_callback_theory.py
Normal file
22
6_tasks/6_12_1_callback_theory.py
Normal file
|
@ -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())
|
22
6_tasks/6_12_callback_theory.py
Normal file
22
6_tasks/6_12_callback_theory.py
Normal file
|
@ -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)
|
Loading…
Reference in a new issue