python_asyncio_course/3_awaitable_objects/task_3_4_12.py
2024-11-02 14:13:39 +03:00

46 lines
1.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import asyncio
async def first_function(x):
print(f"Выполняется первая функция с аргументом {x}")
await asyncio.sleep(1)
result = x + 1
print(f"Первая функция завершилась с результатом {result}")
return result
async def second_function(x):
print(f"Выполняется вторая функция с аргументом {x}")
await asyncio.sleep(1)
result = x * 2
print(f"Вторая функция завершилась с результатом {result}")
return result
async def third_function(x):
print(f"Выполняется третья функция с аргументом {x}")
await asyncio.sleep(1)
result = x + 3
print(f"Третья функция завершилась с результатом {result}")
return result
async def fourth_function(x):
print(f"Выполняется четвертая функция с аргументом {x}")
await asyncio.sleep(1)
result = x**2
print(f"Четвертая функция завершилась с результатом {result}")
return result
async def main():
print("Начало цепочки асинхронных вызовов")
x = 1
x = await asyncio.create_task(first_function(x))
x = await asyncio.create_task(second_function(x))
x = await asyncio.create_task(third_function(x))
x = await asyncio.create_task(fourth_function(x))
print(f"Конечный результат цепочки вызовов: {x}")
asyncio.run(main())