Отдельно разберём 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
🤝#Electra is not just a DEX but your advantage in the market!
Electra will soon open access to a unique trading platform with the best tools for smarter, faster and more efficient trading.
⚡️The main advantages of the future platform:
✔️Artificial intelligence for predicting market trends and automating portfolios.
✔️Freedom and security: none🙅 KYC, up to 1000x leverage and multi-chain trading.
✔️Social features: real-time communication, participation in the Electra Trading League and obtaining unique Proof-of-Trade NFTs.
✔️Access to deep liquidity with CEX-level efficiency in a decentralized environment.
✔️CEX-like performance: Using the #Lumia L2 infrastructure, #Electra delivers lightning-fast trades with tight spreads.
✔️No profit limits: no open interest limits, no funding rate limits, and no arbitrary rules.
✔️Trade 24/7 on web platform, desktop or via Telegram Mini App with professional tools and analytics.
✔️Game-based learning with Bulls'n'Bears — a fun educational game in Telegram Mini App.
🤑 While we wait for the release of the DEX platform, we continue to participate in current activities:
✔️Farm points in Electra MiniApp
✔️Play Bulls'n'Bears
✔️Complete daily tasks
🚀Dive into the world of Electra
#Electra#TON#DEX#NoKYC#Web3