Отдельно разберём 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
#TFUEL/USDT analysis :
#TFUEL is currently forming a symmetrical triangle pattern on the monthly timeframe. The price is showing strong volume, indicating that a breakout from this pattern is likely to occur soon that can continue its upward momentum and test higher levels. A potential gain of around +620% is anticipated from the current level.
TF : 1M
Entry : $0.09330
Target : $0.67520
SL : $0.06149
#TFUEL/USDT analysis :
#TFUEL has bounce back from the support zone and break above the trendline. Price is expected to continue its bullish momentum and test previous highs.
TF : 4h
Entry : $0.06237
Target : $0.06954
SL : $0.05766
#TFUEL/USDT analysis :
#TFUEL is currently consolidating sideways near the resistance zone. The price is unable to break the zone upside and facing resistance. It's expected that the price will continue its bearish momentum from the current zone to test the previous swing low.
TF : 4H
Entry : $0.05740
Target : $0.04363
SL : $0.06297
#TFUEL/USDT analysis -
#TFUEL is in a downtrend on the HTF. The price is expected to continue its momentum, and previous lows will be tested. Before entering a short position, wait for a retracement for entry.
TF : 4H
Entry : $0.06791
Target : $0.06011
SL : $0.07132
#Tfuel Falling Wedge Breakout Has Confirmed Overall Consolidating In Symmetrical Triangle Expecting Move Towards In Descending Trendline In Coming Days And Once Trendline Got Cleared Can Easily Test 0.30$ In Midterm