Отдельно разберём TaskGroup, который пришел на замену gather в Python 3.11.
Ключевые отличия
▫️create_task() возвращает объект asyncio.Task, у которого есть соответствюущие методы управления. То есть у нас больше контроля
▫️это контекстный менеджер, который гарантирует что все таски будут остановлены по выходу из контекста
▫️ошибка автоматически отменяет незавершенные задачи,
▫️except* передает нам ExceptionGroup, в котором каждую ошибку можно обработать отдельно
import asyncio
import random
async def do_it() -> str:
if random.random() < 0.1:
raise ValueError('Oops')
delay = random.uniform(0.5, 1.5)
await asyncio.sleep(delay)
return delay
async def main():
try:
async with asyncio.TaskGroup() as tg:
for _ in range(10):
tasks.append(tg.create_task(do_it()))
for t in tasks:
print(t.result())
except *ValueError as e:
for err in e.exceptions:
print(err)
asyncio.run(main())
Рекомендую изучить страницу Coroutines and Tasks из документации, где представлено больше интересных примеров и механизмов
- таймауты
- отмена задач
- создание задач из другого потока
#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