Отдельно разберём 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
$AIRDROP - the token of @airdrop2049 listed on @gate_io and @LBank_Exchange has been airdropped to #UXLINKseason2 stakers just now, check your wallet! https://arbiscan.io/address/0x0698e377ca9dbfb5c77503d611c6f1f38cf46ba2
For $SONIC on @solana, $INVITE and $DUCK on @ton_blockchain, #UXLINKseason2 stakers please login below page to claim the token. For new stakers, no worries next round #UXLINK eco-airdrop will launch on next month, more tokens are coming! Please stay tuned❤️https://dapp.uxlink.io/stake?tab=airdrop
$UXLINK eco-airdrop is ready🚩super-hot and listed include: $SONIC $DUCK $MOZ $PINGO $OBT $INVITE and $AIRDROP.
$UXLINK is the single and universal token of #UXLINK ecosystem, including UXLINK ONE Chain. This eco-airdrop is our return and empowerment to our $UXLINK holders. We are grateful for your trust and holding of $UXLINK token🤝
👉 This is also a new strategic innovation with our ecological fund
@UFLY_Labs
and many ecological partners. We believe that every token airdrop will bring more user attention, more potential buyers and holders to each project. Let’s keep building, to the moon🚀🌕
Users who participated in staking $UXLINK in #UXLINKseason2 are eligible for this round eco-airdrop.
⏰ Airdrop will be completed before 10:00 UTC on January 24, 2025
$ORB and $MOZ #Airdrops done just now, #UXLINKseason2 stakers please kindly check your wallet address to get the token❤️https://arbiscan.io/address/0x0698e377ca9dbfb5c77503d611c6f1f38cf46ba2