Отдельно разберём 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
🚀 zkSync to Unlock Significant Number of Tokens on April 17
zkSync is set to unlock approximately 17.314 million tokens on April 17 at 0:00 UTC+8. According to ChainCatcher, this release is part of the platform's ongoing token management strategy. The unlocked tokens are valued at around $2.66 million. This development is closely watched by market participants as it may influence the token's market dynamics.
#zkSync#TokenUnlock#Cryptocurrency#Blockchain#MarketUpdate#CryptoNews#ZK
🚀 Immutable X to Unlock 9.62 Million Tokens on April 18
Immutable X (IMX) is set to unlock approximately 9.62 million tokens on April 18 at 8 a.m. UTC+8. According to ChainCatcher, this information comes from Web3 asset data platform RootData's token unlocking data.
#ImmutableX#IMX#TokenUnlock#Web3#Crypto#Blockchain#RootData#ChainCatcher
🚀 Kaito Token Unlock Scheduled for April 20
Kaito is set to unlock approximately 12.11 million tokens on April 20 at 12:00 PM UTC+8. According to ChainCatcher, this token release is part of the Web3 asset data platform RootData's schedule. The unlocked tokens are valued at around $4.86 million.
#Kaito#TokenUnlock#Web3#RootData#Crypto#Blockchain#TokenRelease#April20#KAITO
🚀 Quai Network to Unlock 43.02 Million Tokens on April 19
Quai Network is set to unlock approximately 43.02 million tokens on April 19 at 21:00 UTC+8. According to ChainCatcher, this token release is part of the network's ongoing schedule. The tokens are valued at around $1.25 million. This development is being closely watched by stakeholders and investors in the Web3 asset space.
#QuaiNetwork#TokenRelease#Web3#Crypto#Blockchain#TokenUnlock#Investors#Stakeholders#ChainCatcher