Отдельно разберём 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
У нас много традиций: делать крутые кейсы и исследования, играть настолки в офисе и каждый четверг делиться с командой чем-то полезным на demo day. Этим летом появилась ещё одна — aic.weekend.
aic.weekend — это собраться с любимыми коллегами в парке, принести еду и хорошенько отдохнуть. Заняться йогой, вкусно поесть, попеть и потанцевать под музыку aic.band❤️
Именно так мы провели 13 августа 🙌 Смотрите видео, чтобы зарядиться позитивом ✨
#picnic
Thinking about Father's Day plans? Why not organise a memorable picnic? Here are some tips to make it fun and eco-friendly. #FathersDay#EcoFriendly#Picnic#SayYEStoWasteLess
A mini picnic in the park. 🧺
Just being here and now is the best plan for the day.
Мини-пикник в парке. 🧺
Просто быть здесь и сейчас — лучший план на день.
#LunaEverly#Picnic#DayMindset#Park#Simplicity
✔️Attention all picnic lovers!
📌 We're excited to announce a picnic day at the beautiful BELDERSOY location right after Midterm exams. Get ready to spend a fun-filled day surrounded by lush greenery, serene landscapes and the company of great people.
🤩 The best part? This event is fully free for all to join! That's right, all you need to do is show up with a smile on your face and a willingness to have a good time. And, if you're feeling extra peckish, 😊 feel free to bring along your favorite snacks and drinks to share with others.
Don't miss out on this amazing opportunity to enjoy the great outdoors and connect with like-minded people. Spread the word, mark your calendars, and we'll see you at Beldersoy on May 6th! 🌳🌞🧺
🗓 May 6th (Saturday)
⏰ Departure - 6 AM
Return - 6 PM
Best Regards,
Student Government.
#picnic#Beldersoy#May6th#funinthesun