Отдельно разберём 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
#LINA/USDT analysis :
#LINA is currently in a downtrend, consistently making lower lows while trading below the 200 EMA. The price is now retracing and is anticipated to test the resistance zone before likely resuming its bearish momentum.
TF : 2H
Entry : $0.004192
Target : $0.003430
SL : $0.004597
#LINA/USDT analysis :
#LINA is presently experiencing a pullback, creating a potential short trade opportunity. The price is currently being rejected from the resistance zone after the breakdown and retest. It is probable that it will test the previous swing low.
TF : 15min
Entry : $0.00456
Target : $0.00447
SL : $0.00462
#LINA hit 83🎯
4th target achieved in just 13 hours✅✅✅✅
One more huge profit 33.33%🤑💰🤑
👉 Still thinking? The more you wait more you lose profit
☎️ Contact @FutureExpertAdmin for premium membership and grab next breakout signal
#LINA hit 83🎯
4th target achieved in just 13 hours✅✅✅✅
One more huge profit 33.33%🤑💰🤑
👉 Still thinking? The more you wait more you lose profit
☎️ Contact @FutureExpertAdmin for premium membership and grab next breakout signal
#LINA result
1st target achieved in just 25 minutes✅
One more quick profit 5.2%💰🤑
👉 More quick profit signals available in premium channel. Hurry up 🏃♂👇
☎️ Contact @FutureExpertAdmin
#LINA compressed between the trendline and support zone on Weekly time frame, waiting for a Bullish momentum
📈
❄️@signals_bitcoin_crypto❄️
❄️@Shadow_support0o❄️