Отдельно разберём 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
Auto CPR First Aid Choking Device Outdoor Home Choking #Emergency Equipment Adults Children Breath Rescue Anti Suffocation Device
Buy link amazon 👇
https://amzn.to/40fuhzJ
Bay link Aliexpress 👇
https://s.click.aliexpress.com/e/_EIAiLBO
The US government has revealed a list of items Americans should keep on hand in case of an emergency.
Officials warned that households should be prepared to survive on their own for several days if disaster strikes, with no immediate access to outside help.
The Department of Homeland Security recommends storing enough water, food, and medical supplies to sustain every person in a household during an extended emergency.
At the top of the list is water, at least one gallon per person per day, along with several days' worth of non-perishable food. Americans are also urged to keep critical safety items such as flashlights, radios, batteries, and first-aid kits readily available in a portable emergency supply kit.
Officials said the supplies should be stored in airtight containers and kept in easy-to-carry bins or bags in case families need to evacuate quickly.
The checklist also includes tools to signal for help, sanitation supplies, and equipment to shelter safely indoors if conditions outside become dangerous.
While the Trump administration has not urged Americans to stockpile large quantities of supplies, federal officials and preparedness experts recommend keeping essential items on hand in case of an emergency.
#Security#Emergency
👂More on Trump's Ear ⚠️
On January 19, 1966, Indira Gandhi became India’s first female Prime Minister after Lal Bahadur Shastri died. She led the Green Revolution to increase food production and imposed the Emergency in 1975, limiting freedoms. Her time in office had major changes and controversies.
🌾🇮🇳
[Read more]
@googlefactss#IndiraGandhi#India#History#GreenRevolution#Emergency