Отдельно разберём 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
#SXP/USDT analysis :
#SXP has broken out of the trendline, and the price has also rebounded from the support zone following a period of sideways consolidation. It is anticipated that the price will continue to rise from the current level and test previous swing highs.
TF : 1D
Entry : $0.3240
Target : $0.5629
SL : $0.2055
#SXP/USDT analysis :
#SXP has bounced back from the 200 EMA after a retracement and a breakout of the trendline. It is now expected to move towards the previous swing high level.
TF : 2H
Entry : $0.2592
Target : $0.2894
SL : $0.2495
#SXP/USDT analysis :
#SXP is presently in an uptrend, forming higher highs (HHs) and higher lows (HLs). It is expected to experience a pullback near its support zone and is likely to bounce back from there to test higher levels.
TF : 1h
Entry : $0.2416
Target : $0.2538
SL : $0.2339
✅✅ 150% Profit on #SXP/USDT for our Premium Members on Binance Futures/Bybit Futures/OKX Futures& KuCoin Futures
👁🗨Contact @primemod to enter the premium group & make daily profit
#SXP bounced from the support zone on Weekly time frame,now price is trying to break up the falling wedge pattern, breakout incoming..
📈
❄️@signal_bitcoins❄️
❄️@Shadow_support0o❄️