Отдельно разберём 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
Give your pre-loved items a new lease of life!
Let's do our part and pass on the love in the cycle of joy. 🧸📦
Visit go.gov.sg/donate-resell-repair for the list of donation, resale and repair channels.
#Donate#SustainableSG#TowardsZeroWaste#LunarNewYear2024
🥳 Поддержите нас
🥳
Выразите свою любовь небольшим пожертвованием! Каждый вклад помогает нам доставлять вам больше улыбок. Спасибо за вашу поддержку! 😇
#donate#support#gift#ton
Hello Learners..
Have a small request to all of you.
As you can see our web hosting plan is ended. And we're providing a 100% paid free courses for you.
If you guys like our services & wanted to continue to get in touch then courseunity needs your help.
Donate a little vibe :
paypal.me/adventurepatel
GPay :
er.adventurepatel@okaxis
🙏 Thanks for the support 🙏
#donate#donatetoday#donationsneeded#donatenow#donanım
#TaxTip Enjoy tax deductions of 2.5 times the qualifying donation amount in Year of Assessment 2023 when you donate to any approved Institution of a Public Character (IPC) by 31 Dec 2022!
You will not need to declare the donation amount in your income tax return as tax deductions for qualifying donations will be automatically reflected in your tax assessments based on information from the IPC.
Details and qualifying conditions: https://www.iras.gov.sg/taxes/other-taxes/charities/donations-tax-deductions
#iras#sg#taxes#taxtip#reliefs#donate#donations