Отдельно разберём 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
NGCam | XML
Channel for user xml configs for Google Camera 7.4 by Nikita. You will find all configs created for supported devices.
All files will be send to the bot and posted in the channel according to the requirements.
NOTE: Use hashtags there. Starting with#pinned
For our subscribers who may be too busy/occupied and cannot follow the signals every time, you can take great advantage with the investment service.
The package works perfectly for busy traders, beginners or anyone who doesn't want to take risks while trading personally..
You make an investment with us and we trade for you, after 6 to 10 days we return with profits directly to your account/wallet provided.😍✅
If you're interested, send message
Click on the #pinned message and contact #admin
100% GUARANTEED PROFITS ❤️💯
I assure you, this is an opportunity you'll love🤩
@DeleteEventsBot
Qué puede hacer este bot?
Este bot elimina mensajes de eventos (también conocidos como mensajes de acción) en supergrupos.
Idioma: Inglés
(visto en @BotsGram_cu)
#delete, #service, #message, #spam, #helper, #group, #supergroup, #join, #left, #pinned