Три способа выполнить множество задач с 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
🎉 Exciting Partnership Announcement
We're thrilled to introduce our first official partner for the SingSing SocialFi SDK: Cowtopia🐮!
Cowtopia, an innovative farming simulation game, will be the first to integrate our powerful SocialFi SDK. This collaboration marks a significant milestone as we prepare to revolutionize the Telegram mini-app ecosystem.
Stay tuned and see how Cowtopia will utilize our SDK to create an immersive and socially connected gaming experience.
Read more and open treasure at: X Post
#SingSingSDK#Cowtopia#TelegramGaming#SocialFi#FarmingSimulator#ComingSoon
🔥🎉 Prepare yourself for an unparalleled gaming experience with TonAge! 🎮
💥
😀
💯Bid farewell to losses and say hello to an assured 100% Return to Player (RTP) in TonAge! 💰
✨
😀⭐️ Launch into a world of electrifying gameplay, where each spin, move, and quest is brimming with exhilarating excitement!
💥📍
⚡️
😀 Driven by blockchain technology, TonAge guarantees fair play and transparent outcomes, ensuring you can game with absolute peace of mind! 😎
😀
💎
✨ Unlock legendary treasures, conquer epic challenges, and witness your virtual victories transform into real-world value with the TON cryptocurrency! It's the ultimate gaming revolution!
🎁
💎
🤝⭐️ Join a vibrant community of gamers, strategists, and treasure seekers from all corners of the globe. Share tips, challenge friends, and compete for the top position on the leaderboard! 📍
🎁Don't miss out on this groundbreaking opportunity! Embark on an adventure that guarantees 100% RTP and endless thrills!
💎https://bit.ly/3SlL8fq
#TonAge#TelegramGaming#100PercentRTP#BlockchainGaming#EarnTON