Отдельно разберём 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
#BNX/USDT analysis :
#BNX has experienced a significant drop of approximately 74% from its peak and has found support at a previously respected support zone. The price is currently forming a double bottom reversal pattern at this critical level. It is expected that the price will bounce back from this zone and continue to rise, targeting previous highs.
TF : 1D
Entry : $0.5600
Target : $1.6045
SL : $0.4026
#BNX 30m indicator strategy
👨✈️
GG-Shøt caught the trend at 0.26 - the rest is history /// 😘^^
+4159% max profit (10x lev)
+8318% max profit (20x lev)
The chart speaks for itself.
Others watch
🕺 We execute.
#BNX result
All target achieved in just 6 days ✅
One more huge quick profit 25%🤑💰🤑
👉 Still thinking? The more you wait more you lose profit
☎️ Contact @MichaelStrategiesVip for membership and grab next breakout signal
#BNX result
3 nd target achieved in just 6 house ✅✅✅
One more huge quick profit 18.6%🤑💰🤑
👉 Still thinking? The more you wait more you lose profit
☎️ Contact @MichaelStrategiesVip for membership and grab next breakout signal