Отдельно разберём 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
We start the year with another event — SE Telegram Builders Day in Hong Kong from @epicweb3!🪭
The day before Consensus HK starts, speakers from #Blum, #TerminalStation, #AnimocaBrands, #StormTrade, #Fintopio, #TONFoundation and other projects will gather on one stage.
Participants will discuss the latest trends, attracting users and investments, secrets of viral projects, the future of the ecosystem and much more that awaits us in 2025.
The event is FREE, but places are limited, so hurry if you want to join.
🗓️February 17, Hong Kong
🎫Get a free ticket👈
🤩Fintopio is a new non-custodial wallet in the TON ecosystem
#Fintopio is a cryptocurrency wallet in🪙 Telegram, which can be used via mini app and website. With its help, you can conveniently use #DeFi and #CeFi functions, pay bills, divide expenses, buy goods and transfer money without leaving the application.
To get started quickly, you can import an existing wallet:
🟡Open "DeFi" tab in the Fintopio app
🟡 Click "View all"
🟡 Next – “Import wallet”
🟡 Enter your 12-word secret phrase to import your wallet
👛Currently the wallet supports: #BTC, #USDT [#ERC20/#TRC20], #ETH, #TRX and #TON.
📲 The project has a game to attract an audience where $HP (Hold Points) are farmed. You need to collect coins every 8 hours and in the future you can exchange them for the main utility token of Fintopio named $HOLD.
The token is only available to early participants of the project, the more you #HOLD, the more rewards you will receive from Fintopio.
So it's better to start right now!🚀
Game | Channel | Website