Три способа выполнить множество задач с 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
📚Book Fair at Green University
📖A book fair was held at the university, bringing together students, faculty, and book lovers in a lively academic atmosphere. The event offered a wide selection of literature, encouraged reading culture, and created a space for meaningful exchange of ideas.
#BookFair#ReadingCulture#StudentLife#LoveForBooks
✉️[email protected]
☎️ +998 55 512 00 77
📍Location
🌐Website | 📱Telegram | 📱Instagram | 📱LinkedIn | 🟥YouTube
📚 “Book Brain” Weekly Activities at Green University
Starting this week, weekly activities aimed at promoting reading culture are being launched under the “Book Brain” project.
📖Each week, 3 nominations will be announced:
🏆Best Book Presentation
🔥Most Active Student
📚Top Reader (Most Books Read)
🎁Winners will be selected based on an awarding system and receive special rewards.
🚀If you are passionate about reading and would like to showcase your knowledge, we encourage you to participate.
#GreenUniversity#BookBrain#ReadingCulture#Students#Education __________________
📚“Book Brain” haftalik tadbirlari
Ushbu haftadan boshlab kitobxonlik madaniyatini rivojlantirishga qaratilgan “Book Brain” loyihasi doirasida haftalik tadbirlar yo‘lga qo‘yilmoqda.
📖Har hafta 3 ta yo‘nalish bo‘yicha g‘oliblar aniqlanadi:
🏆Eng yaxshi kitob taqdimoti
🔥Eng faol talaba
📚Eng ko‘p kitob o‘qigan talaba
🎁G‘oliblar maxsus baholash tizimi asosida saralanib, munosib rag‘batlantiriladi.
🚀Agar siz kitob mutolaasiga qiziqsangiz va o‘z bilimlaringizni namoyish etishni istasangiz, ushbu loyihada ishtirok etishga taklif etamiz.
#GreenUniversity#BookBrain#Kitobxonlik#Talabalar#Ta’lim
✉️[email protected]
☎️ +998 55 512 00 77
📍Location
🌐Website | 📱Telegram | 📱Instagram | 📱LinkedIn | 🟥YouTube
📚An event was held as part of the Book Brain project at Green University!
⌛During the event, participants were given a detailed explanation of the project’s coin system, which is designed to evaluate engagement and encourage active participation.
One of the most engaging parts of the event was the “favorite book” presentations. Participants shared insights about the books they love, along with their impressions and key takeaways. This created a space for meaningful discussions and inspired interest in new books.
#BookBrain#ReadingCulture#Event#Students#Education#Motivation#Reading __________________________
📚Book Brain loyihasi doirasida Green University'da tadbir bo‘lib o‘tdi!
⌛Tadbir davomida ishtirokchilarga loyiha mexanizmlaridan biri bo‘lgan coin tizimi batafsil tushuntirildi. Mazkur tizim qatnashchilarning faolligini baholash va ularni yanada faol ishtirok etishiga xizmat qiladi.
Shuningdek, tadbirning eng qiziqarli qismi — “eng sevimli kitob” taqdimotlari bo‘ldi. Ishtirokchilar o‘zlari sevib o‘qigan asarlar haqida so‘zlab berib, kitobdan olgan taassurotlari va xulosalari bilan o‘rtoqlashdilar. Bu esa o‘zaro fikr almashish va yangi kitoblarga qiziqishni oshirishga xizmat qildi.
#BookBrain#Kitobxonlik#Tadbir#Talabalar#Ilm#Motivatsiya#Reading#Education
✉️[email protected]
☎️ +998 55 512 00 77
📍Location
🌐Website | 📱Telegram | 📱Instagram | 📱LinkedIn | 🟥YouTube