Отдельно разберём 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
#GALA/USDT analysis :
#GALA is currently consolidating above the support zone and the 200 EMA. A price bounce is anticipated, allowing for a continuation of its bullish momentum aimed at testing previous highs. Wait for a breakout above the $0.03736 level for a long entry.
TF : 1D
Entry : $0.03736
Target : $0.06650
SL : $0.02860
#GALA/USDT analysis :
#GALA is in an uptrend, forming higher highs and higher lows. After retracing to the 200 EMA and a support zone, the price bounced back and broke the trendline. This suggests a bullish continuation, with expectations to test previous highs.
TF : 15min
Entry : $0.05933
Target : $0.06655
SL : $0.05497
#GALA/USDT Analysis-
After forming a symmetrical triangle pattern, the price broke out, achieving a 30% gain within three days. Currently, the price has completed a retest and is approaching its previous swing high. If it successfully breaks above this level, it has the potential to rally further by over 45%.
T.F.- 1-D
ENTRY- 0.0317
SL- 0.02720
TARGET- 0.04640
Note: If the stop-loss is triggered before entry, disregard the trade as the price action may develop differently.
#GALA/USDT analysis :
#GALA is in an uptrend, making higher highs (HHs) and higher lows (HLs). The price has recently tested the support zone and is expected to bounce back from there to test the previous swing high.
TF : 1D
Entry : $0.01984
Target : $0.02441
SL : $0.01824