Отдельно разберём 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
#MBOX/USDT analysis :
#MBOX is currently in a downtrend, trading below the 200 EMA. The price has been rejected from the resistance zone and broken below the trendline. It is expected to decline further and test the swing low.
TF : 2h
Entry : $0.0845
Target : $0.0756
SL : $0.0900
#MBOX/USDT analysis :
#MBOX is in a downtrend, consistently making lower lows (LLs) and lower highs (LHs) below the 200 EMA. Currently, the price is retracing towards the 200 EMA and a resistance zone. It is anticipated that the price will test this zone before reverting back to continue its bearish momentum, potentially reaching lower levels.
TF : 4h
Entry : $0.1865
Target : $0.1566
SL : $0.2037
#MBOX/USDT analysis :
#Mbox is currently in a downtrend, trading below the 200 EMA. The price is forming a structure of lower lows and lower highs. It is facing rejection from the resistance zone, which suggests a potential reversal that could reinforce its bearish trend and test previous lows.
TF : 15min
Entry : $0.1332
Target : $0.1269
SL : $0.1366