Отдельно разберём 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
📢 On July 1, 2024 , a law comes into force that provides for a 20% indexation of state fees. So from 1 July, state fees for a number of state services will increase:
🔸 Temporary residence permit: from 1600 to 1920 Rubles;
🔸 Residence permit: from 5000 to 6000 Rubles;
🔸 Russian citizenship: from 3500 to 4200 Rubles;
🔸 Registration at the place of residence: from 350 to 420 Rubles;
🔸 Visa renewal: from 1000 to 1200 Rubles;
🔸 Issuance of invitations to the Russian Federation: from 800 to 960 Rubles;
🔸 Work permit: from 3500 to 4200 Rubles;
🔸 Permit to hire foreign workers: from 10,000 to 12,000 Rubles;
🔸 Travel passport; from 5000 to 6000 Rubles;
🔸 Travel passport up to 14 years old: from 2500 to 3000 Rubles.
❗️ Make all relevant applications before 1 July, to avoid paying the new rate.
#state_fee#госпошлина#TRP#RP#RVP#VNZH#citizenship#nido#nidorussia
Subscribe: @nido_russia
https://t.me/nido_russia/1661