Отдельно разберём 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
As we contend with national, regional, and global health security threats, our #NCDCStrategy 2023-2027 captures our collective vision and commitment to deliver on our mandate.
Are you set to join us for the launch?
🗓️Tuesday 21 November 2023
Get notified
🔗bit.ly/NCDCStrategyLaunch
#Vision2Action
Our NCDC 2023-2027 STRATEGY AND IMPLEMENTATION PLAN has been launched and is available for download
Access via
https://ncdc.gov.ng/themes/common/files/establishment/0ee30df42b787ee3bbced29b9930b27f.pdf
#NCDCStrategy#IamHealthSecurity#Vision2Action
We are very excited to present to you:
The Nigeria Centre for Disease Control and Prevention 2023-2027 STRATEGY AND IMPLEMENTATION PLAN
Download via
https://ncdc.gov.ng/themes/common/files/establishment/0ee30df42b787ee3bbced29b9930b27f.pdf
#NCDCStrategy#IamHealthSecurity#Vision2Action
Today, we're taking the next step by launching the NCDC Strategy 2023 to 2027, tagged ‘vision into action’"
Follow the discussion via
https://twitter.com/NCDCgov/status/1726894890158887036?t=Jd4yndV7SWHGomEakivxxA&s=19
#NCDCStrategy#IamHealthSecurity#Vision2Action
The D-DAY has commenced.
Join us now as we kickstart our #HealthSecurity partners meeting and the #NCDCStrategy 2023-2027 launch event.
🔗bit.ly/NCDCStrategyLaunch
#IAmHealthSecurity#Vision2Action
Transforming our #Vision2Action, the #NCDCStrategy story continues 💪🏾
The #NCDCStrategy 2023-2027 is set to be launched❕
Join us for our health security partners meeting and strategy launch.
When❓
🗓️Tuesday, 21 November 2023
#SaveTheDate via
🔗bit.ly/NCDCStrategyLaunch
#IAmHealthSecurity