Отдельно разберём 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
#The_Economist🇬🇧📕[PDF]⬇️
15 #November2025
#Weekly_Magazines
For learning, for free(dom).
@backupofmagazines
This week’s Economist tracks how an emerging #AIBubble, volatile #Markets, and shifting #Geopolitics could reshape 2026. From Taiwan’s hidden economic risks to Mexico’s security crisis and Europe’s democratic strain, global fault lines widen. China faces birth-rate pressures and reputational challenges, while the U.S. wrestles with #Trumpforce, homeschooling, and e-bike parenting. Business pages explore #Chatbots, HR’s rise, and America’s “seven deadly sins.” Finance dives into “recession-recession” fears and personal-finance defence. Science highlights #AI superforecasting and whales’ vowel systems. A dense but essential issue for anyone tracking #Tech, #Policy, and global stability.
#The_Bloomberg🇺🇸📕[PDF]⬇️
#November2025
#Monthly_Magazines
For learning, for free(dom).
@backupofmagazines
This issue pits #GavinNewsom against a shifting #USPolitics map while probing the supposed #AIBubble, from frothy valuations to real-world productivity. A climate-forensics read asks whether #CloudSeeding worsened Dubai’s deadly storm—at the nexus of #ExtremeWeather and #ClimateChange. The geo-economics file tracks India’s wobbling investments, China’s expanding #Ports, and weaponized #SupplyChains. For investors, the issue weighs #ETFs, #401k tweaks, #BigTech concentration risk, and hedges in #Europe and #Gold. Lifestyle detours sample Utah powder and the dive-bar revival. It’s a brisk tour of power, money, and culture in a volatile world. #Markets#Investing#Energy#MiddleEast#Africa#Healthcare#Birkenstocks