Отдельно разберём 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
#RARE/USDT analysis :
#RARE is currently in an uptrend, trading above its 200 Exponential Moving Average (EMA). The price is currently testing the support zone and the 200 EMA. It is expected that the price will bounce back from these levels and will test the swing high. For a long entry opportunity, consider waiting for the price to break above the $0.0930 level.
TF : 2h
Entry : $0.0930
Target : $0.1250
SL : $0.0792
#RARE/USDT analysis :
#RARE is currently undergoing a corrective pullback, anticipated to test the 200 EMA before resuming its bearish momentum. This situation creates a potential long trade opportunity on lower time frame (LTF).
TF : 1h
Entry : $0.0692
Target : $0.0802
SL : $0.0634
#RARE/USDT analysis :
#RARE has broken above the trendline after consolidating in a support zone, indicating a bullish shift. The price is expected to bounce off this zone and rise, testing previous swing highs.
TF : 1D
Entry : $0.1330
Target : $0.2960
SL : $0.0923