Три способа выполнить множество задач с 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
🌟 Applications for YPIP 2026 are NOW OPEN! 🌟
Are you ready to launch your professional journey next summer?
The Young Professional Internships Program (YPIP) provides university students from Uzbekistan the opportunity to strengthen their professional skills through summer internships with leading companies, NGOs, and organizations in Tashkent.
✅ Participants will:
- Gain hands-on work experience in business, STEM, and other fields
- Take part in professional development workshops and field trips
- Network with peers, mentors, and industry leaders
- Receive guidance from established professionals
🌠 Eligibility:
- Commitment to a career in business or STEM
- Citizens of Uzbekistan, currently residing in the country
- Enrolled at a local university as a 3rd or 4th-year student, or graduating by May 2026
- Proficiency in English
⏰ Application Deadline: December 8, 2025
🔗Link to the application
Young Professional Internships Program (YPIP) is funded by the U.S. Embassy in Tashkent and implemented by American Councils for International Education.
#YPIP2026#Internships#YouthOpportunities
🎓Applications for YPIP 2026 are NOW OPEN!
Are you ready to launch your professional journey next summer?
The Young Professional Internships Program (YPIP) provides university students from Uzbekistan the opportunity to strengthen their professional skills through summer internships with leading companies, NGOs, and organizations in Tashkent.
🟣Participants will:
- Gain hands-on work experience in business, STEM, and other fields
- Take part in professional development workshops and field trips
- Network with peers, mentors, and industry leaders
- Receive guidance from established professionals
✔️Eligibility:
- Commitment to a career in business or STEM
- Citizens of Uzbekistan, currently residing in the country
- Enrolled at a local university as a 3rd or 4th-year student, or graduating by May 2026
- Proficiency in English
⏰ Application Deadline: December 8, 2025
📌Link to the application
Young Professional Internships Program (YPIP) is funded by the U.S. Embassy in Tashkent and implemented by American Councils for International Education.
#YPIP2026#Internships#YouthOpportunities