Три способа выполнить множество задач с 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
Multipurpose Court Project
Bishoftu,Ethiopia
Construction on the new basketball court at Hibret (Tokuma) Elementary School is progressing smoothly and is nearly halfway complete. We're excited about the progress and are on track to achieve our goal.
Your support is crucial to ensure the project's successful completion.
Our goal is to create a vibrant playground for the children of Hibret School.
Play is not a privilege; it's a life skill. Every child deserves a place to play!
To contribute and support us please contact us at:
[email protected]
#Playground#AllDeserveToPlay
#Rotaract_Kuriftu
#Rotary
#100yearsofserviceinafrica
#Rotaract
#District9212
#RotaractEthiopia
Follow Us:
Facebook | Twitter | Instagram | Telegram | LinkedIn
On Sunday, February 23, 2025, we will be holding a blood donation, dog adoption, and fundraising program in collaboration with Ahadu Rotaract Club at the Red Cross compound around Addis Ababa Stadium starting 9AM
To those who want to donate:
- Food (Rice) for feeding purpose
- Pet beds
- Comfy blankets
- Medications
- Pet toys
- Dry pet foods
- pet accessaries
We will gladly be waiting, the program will be held until 5pm. Come, save lives by donating blood, create a family by adopting dogs!
#BloodDonation#DogAdoption#CommunityService#AddisAbaba#Ethiopia#SaveLives#GiveBack#AnimalRescue#Rotaract#AhaduRotaract#Fundraising#CharityEvent
https://t.me/starvetbooks
YES! 🙌 It's a wrap! 🥳
The HaraCoders Bootcamp training, brought to you by the awesome Rotaract Club of Kuriftu 🤝, BuildX Ethiopia , Sira Ale, and Addis Ababa University College of Veterinary Medicine and Agriculture , has been a HUGE success!
We're sending out waves of gratitude to everyone involved and a BIG congrats to our fantastic trainees! 🎉
So proud of you all! 🔥
Stay tuned... round 2 might be closer than you think! 😉🚀
#AddisCoders#BuildXEthiopia#TechForAll#EmpowerTheFuture#CodingBootCamp#EthiopianTech#LearnToCode#CodingForChange
#Rotary
#Rackuriftu
#100yearsofserviceinafrica
#Rotaract
#District9212
#RotaractEthiopia
🚀 Exciting News! 🚀
The AddisCoders BootCamp is brought to you by BuildX Ethiopia in partnership with Rotaract Kuriftu, SERRALE, and Addis Ababa University! 🎓💻
Together, we’re empowering Ethiopian youth with hands-on web development skills to build the future of tech! 🔥
Stay tuned for more updates!
#AddisCoders#BuildXEthiopia#TechForAll#EmpowerTheFuture#CodingBootCamp#EthiopianTech#LearnToCode#CodingForChange
#Rotary
#Rackuriftu
#100yearsofserviceinafrica
#Rotaract
#District9212
#RotaractEthiopia