Отдельно разберём 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
#NEO/USDT analysis :
#NEO is currently in an uptrend, having retraced to test a previously respected support zone. The price is expected to rebound from this level and continue its upward movement to retest previous highs.
TF : 1D
Entry : $14.00
Target : $26.21
SL : $10.50
#NEO/USDT analysis :
#NEO is currently approaching support zone. Price is expected to rebound from this zone and resume its bullish momentum to test previous highs.
TF : 2H
Entry : $19.17
Target : $23.80
SL : $17.12
#NEO/USDT analysis :
#NEO is currently in a downtrend, forming lower lows (LLs) and lower highs (LHs) structure. The price has broken the trendline and is expected to rise higher during the correction phase, testing the previous swing high.
TF : 1h
Entry : $9.24
Target : $9.61
SL : $8.99
#NEO/USDT analysis :
#NEO is presently in a downtrend, trading below the 200 EMA. The price is anticipated to maintain its bearish momentum and revisit previous lows. To contemplate initiating a short position, it is recommended to wait for a retracement and a retest of the established resistance level.
TF : 4h
Entry : $24.05
Target : $9.73
SL : $10.03