Отдельно разберём 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
⚠️
⚠️
⚠️
⚠️
⚠
🔔AMA ANNOUNCEMENT
🗣We are pleased to announce our #Voice AMA with "Ethernity Cloud " || 16th Jan 2025 at ( 13:00PM UTC)
🔠
🔠
🔠
🔠
🔠
🔠
🔠
🔠
⏳ Time : 16th Jan, 2025 at (13:00PM UTC)
🏠 Venue
✉️Crypto Fight
🤵♂️Guest : Iosif Peterfi - CEO
➡️There will be 3 segments. Last approximately for 45 minutes - 1 hours.
🟢Segment 1: Introduction Questions
🟢Segment 2: Twitter Questions
🟢Segment 3: Live Questions
📌Rules to follow AMA:
✅ Join
✉️CryptoFight &
✉️Ethernity-cloud
✅Like & Retweet
✅Twitter Post
✅ Follow
📝CryptoFight &
📝Ethernity-cloud on Twitter
📝 Ask Your Questions ( Here )
⏰ Must Join "Ethernity Cloud" Social Media Links :
✅Website✅Telegram✅Twitter✅Discord🌐Github✅Youtube✅Facebook
⏰ Must JoinCrypto Fight Community :
✉️Telegram
✉️Channel
📝Twitter
🔶Binance Live
*️⃣Linktree
💥If You Want To Promote Your Project Or AMA Proposal
📞DM @Ryan_Fight
Tribute: Voice Actor Shares Success Story
#Tribute#voice
Tribute shares insights from a voice actor who has mastered monetizing talent using the platform. Learn how they turn their passion for voice acting into income.
Source: link
@tonlines
🆕 AI News:Did you know? Scientists recently trained an AI to recognize emotions in human voices even better than some people can! Experts believe this could help build smarter assistants and even help diagnose health issues sooner.
❕ Would you trust an AI to sense your mood?
➖➖➖➖🔻
🧠 BOT: @Chatgpt_OfficialBOT
💎@Chatgpt_OfficialNews
#️⃣#AI#Voice#News
➖➖➖➖🔺