Отдельно разберём 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
🌍 Time zones are not always set by longitude—some countries, like China, use one time zone nationwide despite spanning five natural zones, affecting daily life from sunrise to sunset across vast distances. ✨
#timezones⚡#clocks⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 Some time zones offset by just 15 or 30 minutes exist, like India’s UTC+5:30 and Newfoundland’s UTC−3:30. These unusual divisions reflect local choices instead of sticking to whole hours. ✨
#timezones⚡#clocks⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography🌍
🌍 Some countries adjust their time zones by just a single minute. Venezuela, for example, once set its clocks to UTC−4:30, then shifted to UTC−4:00 for efficiency, creating rare minute-level changes. ✨
#timezone⚡#clocks⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 Spain's Canary Islands, though just west of Africa, use the same time zone as mainland Spain. Their clocks are always one hour behind the rest of the country, bridging continents by time. ✨
#timezone⚡#islands⚡#clocks⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 Some Antarctic research stations use New Zealand’s time zone, while others choose the time of their home country. This means clocks at the South Pole can show different times—sometimes up to 12 hours apart. ✨
#timezones⚡#antarctica⚡#clocks⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels
🌍 Nepal is one of the few countries that advances its clocks by 45 minutes from standard time, using a unique offset of UTC+5:45 to better match local solar time. ✨
#timezones⚡#Nepal⚡#clocks⚡#geography⚡#nature⚡#earth
👉subscribe Amazing Geography
👉more Channels