Отдельно разберём 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
#ENS is rebounding from the support trendline of the descending channel, with the Ichimoku Cloud acting as a resistance barrier.
A decisive breakout above both the channel and the cloud would confirm bullish momentum.
#ENS/USDT analysis :
#ENS is currently in an uptrend, consistently reaching new highs while trading above the 200 EMA. The price is now retracing towards the 200 EMA and a significant support level. It is expected that the price will test this zone and rebound, which should support the continuation of bullish momentum and will lead to a retest of previous highs.
TF : 1D
Entry : $30.35
Target : $47
SL : $23.38
#ENS/USDT analysis :
#ENS is currently in an uptrend, trading above the 200 EMA. The price has recently bounced back from the 200 EMA, suggesting a continuation of its bullish momentum and potential testing of higher levels. For a long entry, it is advisable to wait for a retracement to optimize the entry point.
TF : 30min
Entry : $43
Target : $48
SL : $38