39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
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())
|