Отдельно разберём 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
Just In: World Liberty Finance ( @worldlibertyfi ) sent 500M $WLFI worth $83.12M to #Jump Trading.
Data - @Nansen_ai
https://x.com/OnchainLens/status/2010748644979577089
Follow @onchainlens for more onchain updates
Shoutout to the early donators 🚀💥 You’re our real VIPs for jumping in first and supporting the new ETH memecoin season! 🙌💰
The more you #JUMP into $JUMP, the closer we get to make ETH great again 🌕🚀
Let’s f***ing jump and save Ethereum together!
Whales are starting to #JUMP into our donation 🐳🐳
A 70 #ETH donation just came in! It came from a very old and large wallet holding over 2500 ETH, the #MEGA movement is getting stronger by the day. Join the movement and make memes on Ethereum great again!
https://etherscan.io/tx/0x25b8397cda09027cc3bcc86ff2abf58fa2dc07022204dd8161a7b2630e658b7d
🚀 The wait is over - $JUMP is live! 🚀
Donate now and secure your stake in Jump.fun, the next-generation memecoin launchpad:
🔥 Liquidity War Mode (loot liquidity, win big!)
💰 FREE 6 ETH liquidity loan on Uniswap
⚡️ Community-ownership via $JUMP token
#jumpfun is the key to bring back memecoin FOMO and fun to the Ethereum ecosystem. #MEGA
🌕 Don’t just watch - #jump high, moon higher!
Crypto Summit Guest List Revealed
Notable figures like Kayne West and the Lazarus Group are invited to a major crypto summit. In related news, Mexican billionaire Ricardo Salinas has raised his Bitcoin holdings to 70%, moving away from gold investments. Meanwhile, Jump is resuming its U.S. crypto operations, boosting hiring amid rising trading volumes.
#Crypto#Bitcoin#Summit#Finance#Investing#Market#Jump#Arbitrum#LazarusGroup#Salinas