Три способа выполнить множество задач с asyncio
Функция для примера:
async def do_it(n):
await asyncio.sleep(random.uniform(0.5, 1))
return n
1. Последовательный вызов
async def main():
for i in range(100):
result = await do_it(i)
Такой вызов имеет смысл только тогда, когда результат одной задачи требуется для вызова следующей.
Если они независимы, то это антипаттерн, так как аналогичен простому синхронному вызову по очереди.
2. Упорядоченный результат
async def main():
tasks = [do_it(i) for i in range(100)]
results = await asyncio.gather(*tasks)
Выполняет корутины конкурентно и возвращает результат в виде списка.
Полезен когда требуется получить результаты в том же порядке в котором задачи отправлены.
3. Результат по мере готовности
tasks = [asyncio.create_task(do_it(i)) for i in range(100)]
for cor in asyncio.as_completed(tasks):
result = await cor
Так же выполняет корутины конкурентно, но не гарантирует порядок. Результат возвращается по мере готовности, каждый отдельно.
Полезен когда нужно обработать любой ответ как можно скорее.
#async
🚨3️⃣3️⃣3️⃣3️⃣4️⃣4️⃣4️⃣4️⃣4️⃣
Dear Prospective students,
We would like to remind you that just 3 days left to submit the applications for the IUT's second entrance exams. Thus, do not miss out the opportunity! Hurry up to register and get ready to embark on this extraordinary academic journey with IUT!
📱Application platform
🖱
⏳Deadline: June 14 (Friday) by 5 PM
🎬Application process tutorial
🔔Those applicants who got already registered, are kindly asked to pay the application fee using your Payment ID (sent to your email) via 🏦 , 💸 , 💸 appsby June 17, 5 PM to be able to take the entrance exam.
For more detailed information:
📞71 289 99 99 (0400)
✈️@askinhabot
Best regards,
IUT Admissions Team
#IUT#Admissions2024#ApplicationDeadline
☄️🔠🔠🔠🔠🔠🔠🔠🔠🔠🔠🔠🔠🔠🔠🔠☄️
Attention all prospective students! Please kindly note that the deadline for the application submission for the second entrance exams of IUT is today, June 14, by 5 PM.
This is a last chance for those who want to register for the exam now or re-submit the application in case any mistakes are found after reviewing.
📱Application platform
🖱
Please, don't hesitate to reach out to us if you have any last-minute questions or need assistance with your application via:
📞71 289 99 99 (0400)
✈️@askinhabot
➖➖➖➖➖➖➖➖➖➖
🎬Application process tutorial
Best of luck on your application process and exams!
IUT Admission Team
#IUTAdmissions2024#ApplicationDeadline#ProspectiveStudents#GoodLuck
☄️🔠🔠🔠🔠🔠🔠🔠🔠🔠🔠🔠🔠🔠🔠🔠☄️
Attention all prospective students! Please kindly note that the deadline for the application submission for the second entrance exams of IUT is today, June 14, by 5 PM.
This is a last chance for those who want to register for the exam now or re-submit the application in case any mistakes are found after reviewing.
📱Application platform🖱
Please, don't hesitate to reach out to us if you have any last-minute questions or need assistance with your application via:
📞71 289 99 99 (0400)
✈️@askinhabot
➖➖➖➖➖➖➖➖➖➖
🎬Application process tutorial
Best of luck on your application process and exams!
IUT Admission Team
#IUTAdmissions2024#ApplicationDeadline#ProspectiveStudents#GoodLuck