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

39 lines
1.3 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
# Полный словарь students вшит в задачу, вставлять его не нужно
# students = {}
students = {
"Алекс": {"course": "Асинхронный Python", "steps": 515, "speed": 78},
"Мария": {"course": "Многопоточный Python", "steps": 431, "speed": 62},
"Иван": {"course": "WEB Парсинг на Python", "steps": 491, "speed": 57},
}
async def study_course(student, course, steps, speed):
print(f"{student} начал проходить курс {course}.")
reading_time = round(steps / speed, 2)
await asyncio.sleep(reading_time)
print(f"{student} прошел курс {course} за {reading_time} ч.")
async def main():
tasks = []
# Создание задач с помощью asyncio.create_task для каждого студента
for student, student_data in students.items():
tasks.append(
asyncio.create_task(
study_course(
student,
student_data["course"],
student_data["steps"],
student_data["speed"],
)
)
)
# Ожидание завершения каждой задачи индивидуально
for task in tasks:
await task
asyncio.run(main())