Отдельно разберём 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
#MKR/USDT analysis :
#MKR is currently in a downtrend. It is advisable to wait for the price to retrace and test the resistance zone for a short entry. The price is expected to resume its bearish momentum and test lower levels.
TF : 4H
Entry : $1,074
Target : $778
SL : $1,209
#MKR/USDT analysis :
#MKR is in a downtrend, forming lower lows (LLs) and lower highs (LHs). The price is anticipated to continue its bearish momentum and test lower levels. It is recommended to wait for a pullback for a short entry.
TF : 2h
Entry : $1,283
Target : $1,006
SL : $1,426
#MKR/USDT analysis :
#MKR is currently consolidating in a channel. The higher timeframe direction shows bearish sentiment, hinting at a possible breakout from the channel to keep moving downwards to lower levels. Waiting for a channel break and the $1,884 level before considering a short entry is recommended.
TF : 2H
Entry : $1,884
Target : $1,576
SL : $2,045
#MKR | Failure Swing detected by CoinLegs Algorithm 😎
A quick and risky position can be opened from the neck area.
Personally, I would be a bit more cautious and wait for the price to come down and rise from there.
Dyor, NFA.
www.coinlegs.com
💰#MKR pullback done,now we are waiting for a bullish momentum from the Green zone,pump it to the moon 🚀
❄️@signals_bitcoin_crypto❄️
❄️@Shadow_support0o❄️